Delete The TableView Cell

Published on Author bloggerten

Create the button and write the action:

– (IBAction) EditTable:(id)sender
{
if(self.editing)
{
[super setEditing:NO animated:NO];
[tableView1 setEditing:NO animated:NO];
[tableView1 reloadData];
[edit_btn setTitle:@”Edit”];
}
else
{
[super setEditing:YES animated:YES];
[tableView1 setEditing:YES animated:YES];
[tableView1 reloadData];
[edit_btn setTitle:@”Done”];
}
}
Tableview cell  editing Delegate methods:

– (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

if (self.editing && indexPath.row == ([cityArray count]-1)) {

return UITableViewCellEditingStyleDelete;
} else {
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleNone;

}

– (void)tableView:(UITableView *)tableView1 commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{

if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete object from database
[cityArray removeObjectAtIndex:indexPath.row];
[tableView1 deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}

 

Comments

comments

Categories IOS