iphone SDK: How to use the checkmark to delete the row is checked?
I only know the first step is to check a row.
Next I think is use a NSMutableArray
to record which row is been checked,
and this is my code :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([[tableView cellForRowAtIndexPath:indexPath] accessoryType] == UITableViewCellAccessoryCheckmark){
[[tableView cellForRowAtIndexPath:indexPath] setAccess开发者_如何学GooryType:UITableViewCellAccessoryNone];
}
else {
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
}
}
So, my question is:
How to put this checked row's indexPath into a Array?
How to add a delete button that I can delete all the row I selected?
First off declare a NSMutableArray in youre viewController's .h file.
Like so:
NSMutableArray *checkedRows;
Change youre didSelectRow method like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([checkedRows containsObject:indexPath]) {
[checkedRows removeObject:indexPath];
}
else {
[checkedRows addObject:indexpath];
}
[self.tableView beginUpdates]; //Just for animation..
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
}
And in youre cellForRow method add this:
if ([checkedRows containsObject:indexPath]){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
You are going about this wrong.
You don't handle the editing accessory views yourself. Instead, you send setEditing:animated:
to the table and it alters the cells for you. Then you need to implement:
– tableView:commitEditingStyle:forRowAtIndexPath:
– tableView:canEditRowAtIndexPath:
... to actually remove the row cells.
精彩评论