UITableView行插入,删除,更新

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

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

我已经在计算器中search它,但我不明白他们解释。 所以任何人都可以提供教程或示例应用程序插入,删除和更新ios中的UITableView行。

提前致谢。

当你添加新的单元格时,像这样将你的单元格数据插入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]; 

使用此数组将自动sorting,并且“Joy”名称将插入在ZERO索引和数组自动移位

在这之后重新加载你的tableview

 [tableviewObj reloadData]; 

试试这个希望你会获得成功。 对于iOS中的完整表示例UITableView示例

  • 对于删除行检查它.. 如何从UITableView删除选定的行
  • 对于更新行检查它.. 刷新单个UITableViewCell

    在更新之前重新加载你的表中replace你的数组中的date

    [dataArray replaceObjectAtIndex:index withObject:newObject];

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

这三种方法可以用来删除,插入和更新表格行。 这里的苹果代码insertRowAtIndexPaths 点击这里