在自定义单元格的TableView的任何索引处插入一行

我是iOS的新手。 我尝试了更多的时间来search,但结果不是我的愿望。 首先,我有5行与自定义单元格。 单元格仅包含文本,我想在包含一个图像的单元格的任何索引处添加一行。 那么我们如何才能实现呢?

从评论我来了解你是非常新的UITableView。 所以请通过一些给定的教程。 您将能够添加,删除UITableView中的编辑单元格

UITableView教程
教程2
表格视图编程指南

为了简单的插入就像这样

将button及其操作添加到视图

-(IBAction)addNewRow:(UIButton *)sender { self.numberOfRows++; // update the data source NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; [self.tableview insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; } 

编辑

我想你需要这样的事情

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *cellID1=@"CellOne"; NSString *cellID2=@"CellTwo"; UITableViewCell *cell = nil; if (0 == indexPath.row) { cell =[tableView dequeueReusableCellWithIdentifier:cellID1]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID1]; } cell.textLabel.text = @"New Cell"; } else { cell =[tableView dequeueReusableCellWithIdentifier:cellID2]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID2]; } cell.imageView.image = [UIImage imageNamed:@"Test.jpg"]; } return cell; } 

不要忘记增加细胞计数,即现在从numberOfRows返回6。

您可以从视图控制器中的任何位置将行插入到表中

 [tableView beginUpdates]; [tableView insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation]; [tableView endUpdates]; 

给出适当的indexPaths和行animation。

在你的tableView:cellForRowAtIndexPath:方法中,区分你希望单元格与图像的索引path并将其添加到那里,否则只设置UITableViewCell的文本。