• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
禾曰呈天
博客园    首页    新随笔    联系   管理    订阅  订阅

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;

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

 1 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
 2 {
 3     if (editingStyle == UITableViewCellEditingStyleDelete) {
 4         // Delete the row from the data source
 5         if (indexPath.row == 0) {
 6             [self.modeInfomation removeObjectAtIndex:indexPath.section];
 7             [self.modeNameArr removeObjectAtIndex:indexPath.section];
 8             NSIndexSet *sectionIndex = [NSIndexSet indexSetWithIndex:indexPath.section];
 9             [self.tableView deleteSections:sectionIndex withRowAnimation:UITableViewRowAnimationLeft];
10         }
11         else {
12             [[self.modeInfomation objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];
13             [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
14         }
15     }   
16     else if (editingStyle == UITableViewCellEditingStyleInsert) {
17         // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
18     }   
19 }

在普通模式下:

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

注意:

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

posted @ 2014-07-20 14:20  禾曰呈天  阅读(2925)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3