Ios 使用自定义单元格在TableView中的任意索引处插入一行

Ios 使用自定义单元格在TableView中的任意索引处插入一行,ios,tableview,Ios,Tableview,我是iOS的新手。我尝试了更多的时间来搜索,但结果不是我想要的。第一,我有5行自定义单元格。单元格只包含文本,我想在包含一个图像的单元格的任何索引中添加一行。那么我们如何实现它呢?在您的tableView:cellForRowAtIndexPath:方法中,区分要在其中添加图像的单元格的索引路径,否则只设置UITableViewCell的文本。您可以从视图控制器的任何位置将行插入表中 [tableView beginUpdates]; [tableView insertRowsAtIndexPa

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

在您的
tableView:cellForRowAtIndexPath:
方法中,区分要在其中添加图像的单元格的索引路径,否则只设置UITableViewCell的文本。

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

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

提供适当的
索引路径
和行动画。

从评论中我了解到您对UITableView非常陌生。因此,请阅读一些给定的教程。您可以在UITableView中添加、删除编辑单元格



对于简单的插入,请执行以下操作

 - (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;
}
将按钮及其操作添加到视图中

 -(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。

用于显示文本标签和单个图像,无需使用自定义单元格。可以使用默认单元格。你的问题不清楚。你到底想要什么。在表格视图中插入一个单元格或使用不同类型的单元格实际上,我使用包含文本的自定义单元格,然后我想在这个自定义单元格中添加任何内容。要插入新行,我只需要单元格中的图像,但我不知道必须使用相同或不同的单元格。默认情况下,UITableViewCell与图像视图、文本标签和详细文本标签相关联。如果您的单元格看起来像这样,那么您可以使用自定义单元格,这样您就可以根据自己的需要对其进行自定义。当单元格外观不同时,使用不同的自定义单元格。所有这些都取决于你的需要。首先,你必须尝试一些东西。然后出现了一些问题,请在这里询问您的code@Anil如果我想在索引1处添加行,我们该怎么做?在方法didSelectRowAtIndexPath或cellForRowAtIndexPath中写入?何时添加行?当你们触摸某个按钮时?例如,若你们按下一个按钮,那个么应该添加一行,在这种情况下,我们应该在按钮动作中写下这一行。这里的tableView是您在代码中提供的tableView名称。我不使用按钮操作来添加行。我已经有5行了,但我想再添加一行。然后增加行数并在cellForRowAtIndexpath中提供适当的数据。如果我们想直接在TableView中添加此行,我们应该怎么做?我不使用按钮操作添加行。我已经有5行了,但我想再添加一行。