zoukankan      html  css  js  c++  java
  • 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!两个都要更新!

  • 相关阅读:
    字符串最大最小表示法模板 ( 字典序最大最小 )
    Manacher模板( 线性求最长回文子串 )
    2017南宁网络赛 Problem J Minimum Distance in a Star Graph ( 模拟 )
    字符串截取模板 && POJ 3450、3080 ( 暴力枚举子串 && KMP匹配 )
    HDU 6153 A Secret ( KMP&&DP || 拓展KMP )
    51Nod 1277 字符串中的最大值 ( KMP && DP )
    HDU 4300 Clairewd's message ( 拓展KMP )
    拓展KMP以及模板
    KMP解决字符串最小循环节相关问题
    序列终结者 BZOJ 1251 Splay
  • 原文地址:https://www.cnblogs.com/hereiam/p/3856188.html
Copyright © 2011-2022 走看看