在TableView中的任何索引处使用自定义Cell插入一行

2 tableview ios

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

Ani*_*ese 7

从评论中我知道你是UITableView的新手.所以请仔细阅读一些给定的教程.您将能够在UITableView中添加,删除编辑单元格

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

对于简单插入,请执行此操作

向视图添加按钮及其操作

 -(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];

 }
Run Code Online (Sandbox Code Playgroud)

编辑

我觉得你需要这样的东西

 - (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;
}
Run Code Online (Sandbox Code Playgroud)

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