【问题标题】:UITableView rows insertion, deletion, updation [closed]UITableView 行插入、删除、更新[关闭]
【发布时间】:2014-09-24 13:15:18
【问题描述】:

我正在创建一个 ios 应用程序,我想在其中添加每个新行到 UITableView 的顶部,但不应删除已经存在的行。

例如,我在第 1 行有一个单元格,当我单击它时,它会显示图像,即使我们在第 1 行插入一个新单元格并将旧单元格移到第 2 行,也应该显示相同的图像。

我在 stackoverflow 中搜索过它,但我无法理解他们的解释。所以谁能提供教程或示例应用程序在ios中插入、删除和更新UITableView中的行。

提前致谢。

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    当您添加新单元格时,然后像这样在 ZERO 索引上插入您的单元格数据

    NSMutableArray *dataArray = [[NSMutableArray alloc]initWithObjects:@"Hello",@"How Are You", nil]; 
    
    // It's your data array that you have already allocation in viewDidLoad or some where else.. 
    
    
    [dataArray insertObject:@"Joy" atIndex:0];  // Try this line on add there your are adding new cell data
    
    [tableView beginUpdates];
    NSIndexPath *indexpath = [NSIndexPath indexPathforRow:0 inSection:0];
    NSArray *indextoadd = [NSArray arrayWithObject:indexpath1];
    [tableView insertRowsAtIndexPaths:indextoadd withRowAnimation:UITableViewRowAnimationAutomatic];
    [tableView endUpdates];
    

    使用此数组将自动排序,“Joy”名称将插入零索引和数组自动移位

    在此之后重新加载您的表格视图

    [tableviewObj reloadData];
    

    试试这个希望你会成功。完整表格示例UITableView example in iOS

    【讨论】:

    • 如果它解决了您的问题,请接受答案...
    • 删除和更新怎么样
    • 我已经更新了我的答案检查它..
    • 但是通过使用您的代码进行插入,它会在表格底部添加行而不是第一行
    • 但是我已经测试过了,它添加了数组的零索引,那么它怎么能显示在最后...
    【解决方案2】:
    - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
    - (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
    - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
    

    这三种方法可以用于删除、插入和更新表格行。 这里插入RowAtIndexPaths的Apple代码click here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-12
      • 1970-01-01
      相关资源
      最近更新 更多