UITableView中Cell和section的插入与删除

插入段:

- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

插入行:

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

如果在编辑模式下,复写此方法:

 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
if (indexPath.row == ) {
[self.modeInfomation removeObjectAtIndex:indexPath.section];
[self.modeNameArr removeObjectAtIndex:indexPath.section];
NSIndexSet *sectionIndex = [NSIndexSet indexSetWithIndex:indexPath.section];
[self.tableView deleteSections:sectionIndex withRowAnimation:UITableViewRowAnimationLeft];
}
else {
[[self.modeInfomation objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}

在普通模式下:

 [self.tableView beginUpdates];
[[self.modeInfomation objectAtIndex:tag] addObject:@"hehe"];
NSIndexPath *indexpath = [NSIndexPath indexPathForRow:row + inSection:tag];
[self.tableView insertRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationRight];
[self.tableView endUpdates];

注意:

插入或者删除行或段,先更新datasource,再insertRows或insertSections!两个都要更新!

上一篇:ubuntu安裝postman遇到問題


下一篇:RadioGroup&RadioButton