pash3r
pash3r

Reputation: 161

trouble with deleting uitableview rows by default method

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.

Upvotes: 0

Views: 509

Answers (3)

pash3r
pash3r

Reputation: 161

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

Upvotes: 0

08442
08442

Reputation: 521

#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

Upvotes: 2

LJ Wilson
LJ Wilson

Reputation: 14427

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];
}

Upvotes: 1

Related Questions