1

I have a table with 7 section and in each section 2 rows, I don't know why my delete button doesn't work!! and not appear in cells

would you please help me! Thanks in advance!

enter image description here

day.m

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    arry = [[NSMutableArray alloc] init];
    [arry addObject:@"test"];
    [arry addObject:@"hey"];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNewItem)];
    self.navigationItem.rightBarButtonItem = rightButton;
}

-(void)addNewItem
{
    [arry addObject:@"New Day"];
    [self.tableView reloadData];
}

// ... standard methods to override

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 7;
    //_week.days.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [arry count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.textLabel.text = [arry objectAtIndex:indexPath.row];
    return cell;
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if(section == 0)
    {
        return @"Monday";
    }
    else if(section == 1)
    {
        return @"Tuesday";
    }
    else if(section == 2)
    {
        return @"Wednesday";
    }
    else if(section == 3)
    {
        return @"Thuesday";
    }
    else if(section == 4)
    {
        return @"Friday";
    }
    else if(section == 5)
    {
        return @"Saturday";
    }
    else
    {
        return @"Sunday";
    }
}

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        // [self.monthTitle removeObjectAtIndex:indexPath.row];
        [arry removeObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation: UITableViewRowAnimationFade];
    }
    else if (editingStyle==UITableViewCellEditingStyleInsert)
    {   
    }   
}

Edit1: after removing multiple selection from storyboard delete button appears but when I click on delete button it's terminated

enter image description here

1
  • but when I click on delete button it's terminated.. Means ?? Commented Jul 31, 2013 at 8:23

3 Answers 3

1

You didn't begin your table updates. When you use the method "deleteRowsAtIndexPath", you must insert it between this methods:

// Begin update
[tableView beginUpdates];

// Perform update
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
                 withRowAnimation:UITableViewRowAnimationFade];

// End update
[tableView endUpdates]
Sign up to request clarification or add additional context in comments.

1 Comment

Not true, you only need to do that if you want to group multiple edits.
1

From your screenshot, it looks like you've set allowsMultipleSelectionDuringEditing to YES (perhaps in your nib file, as I can't see that in the code). When that's the case, tableView:commitEditingStyle:forRowAtIndexPath: will never be called, because the checkmarks only indicate the user's selection, not that these rows should be deleted.

You need a delete button of your own that is activated when you enter editing mode, similar to how you see it in the Mail app.

2 Comments

that's right right know I have delete button but when I use it my proces terminated
The problem is that you use the same array for all sections.
0

you should first remove row from the datasource like this,

            [self.table_favpropertylist_array removeObjectAtIndex:indexPathRow];
            [tbl_FavDetails beginUpdates];

            UITableViewCell *cell=[tbl_FavDetails cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPathRow inSection:0]];

            NSIndexPath *indexPath = [tbl_FavDetails indexPathForCell:cell];
            [tbl_FavDetails deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                                  withRowAnimation:UITableViewRowAnimationFade];
            [tbl_FavDetails endUpdates];

where self.table_favpropertylist_array is datasource ,tbl_FavDetails is tableview and indexPathRow is index at which row delete button is clicked,

Thanks.

Comments

Your Answer

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