0

This UITableViews gonna make me crazy!

I have UITableViewCell created by

- (UITableViewCell *)tableView:(UITableView *)tableView 
cellForRowAtIndexPath:(NSIndexPath *)indexPath {

doneButton = [[UIButton alloc]initWithFrame:CGRectMake(7, 15, 30, 30)];
[cell addSubview:doneButton];

cell.selectionStyle = UITableViewCellSelectionStyleNone;
[doneButton addTarget:self action:@selector(done:) forControlEvents:UIControlEventTouchDown];

UILabel *postedTime = [[UILabel alloc]initWithFrame:CGRectMake(240, 4, 60, 15)];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
postedTime.backgroundColor = [UIColor clearColor];
[cell addSubview:postedTime];
cell.textLabel.textColor = [UIColor blackColor];

UILabel *post = [[UILabel alloc] initWithFrame:CGRectMake(45, 25, 190, 30)];
[cell addSubview:post];

UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(45, 4, 180, 15)];
[cell addSubview:title];

mission *m = (mission *)[missionsArray objectAtIndex:indexPath.row];
title.text = m.title;
post.text = m.post;
postedTime.text = m.posted;

.h file:

IBOutlet UITableView *table;
NSMutableArray *missionsArray;

I'm trying to delete row by method

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [missionsArray removeObjectAtIndex:indexPath.row];
        [table reloadData];
}

And when I click "Delete" button EXC_BAD_ACCESS. I've tried to call [missionsArray removeAllObject]; -- the same error. Then I've tried to NSLog(@"%u", indexPath.row); -- it prints right rows (when I select first row, it returns 0 and so on). Also I've tried this

if (editingStyle == UITableViewCellEditingStyleDelete) {
        [missionsArray removeObjectAtIndex:indexPath.row];
        [table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
        [table reloadData];
}

And I have no good result. Maybe cell height affects to it... Help, please. I have no idea what to do, where to search.


Problem solved. Trouble was in logical mistake when I tried to release my mission objects.

3
  • 1
    not related to your question...but is there a reason why do you "create" the button and extra labels every time, instead of just once? Commented Sep 7, 2012 at 10:25
  • maybe it's my logical mistake Commented Sep 7, 2012 at 10:27
  • since all your cells are the same and "reusable", move all the init code for buttons and label inside the if (cell == nil) { block, it will improve performance, (better create subclass to keep things tidy) Commented Sep 7, 2012 at 10:30

3 Answers 3

2
#import "ViewController.h"


@implementation ViewController

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

    #pragma mark - View lifecycle

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        missionsArray=[[NSMutableArray alloc]initWithObjects:@"A",@"B",@"C", nil];
    }

    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }


    #pragma mark TableView delegate method.
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView1 {
        return 1;
    }


    - (NSInteger)tableView:(UITableView *)tableView1 numberOfRowsInSection:(NSInteger)section 
    {
        return [missionsArray count];
    }


    - (CGFloat)tableView:(UITableView *)tableView1 heightForRowAtIndexPath:(NSIndexPath *)indexPath{

        return 44;


    }
    - (UITableViewCell *)tableView:(UITableView *)tableView 
             cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        static NSString *CellIdentifier = @"myCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {

            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
        }

       UIButton * doneButton = [[UIButton alloc]initWithFrame:CGRectMake(7, 15, 30, 30)];
        [cell addSubview:doneButton];

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [doneButton addTarget:self action:@selector(done:) forControlEvents:UIControlEventTouchDown];

        UILabel *postedTime = [[UILabel alloc]initWithFrame:CGRectMake(240, 4, 60, 15)];
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
        postedTime.backgroundColor = [UIColor clearColor];
        [cell addSubview:postedTime];
        cell.textLabel.textColor = [UIColor blackColor];

        UILabel *post = [[UILabel alloc] initWithFrame:CGRectMake(45, 20, 190, 30)];

        post.text=[missionsArray objectAtIndex:indexPath.row];
        [cell addSubview:post];

        UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(45, 4, 180, 15)];
        [cell addSubview:title];

        return cell;

    }

        enter code here

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            [missionsArray removeObjectAtIndex:indexPath.row];
            [table reloadData];
        }
    }




    @end
Sign up to request clarification or add additional context in comments.

5 Comments

I'm populating missionsArray like in your answer and my UItableView has 1 section. It's not working :\
oh sorry I'm blind. Moment I'll check it.
i think u have not connected the table in xib check it out once
right. It was not connected. But after connection -- nothing happens. EXC_BAD_ACCESS
please copy all the delegate methods of the table view i have given and paste it there it will work please keep the break point and check once
1

Assuming you have only one section in your TableView Try this:

if (editingStyle == UITableViewCellEditingStyleDelete) {
        [missionsArray removeObjectAtIndex:indexPath.row];
        [table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        // No need to reload the tableview.  Remove this line entirely [table reloadData];
}

Comments

0

Thanks for help. I solved the problem. Answer is in the updated question!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.