我是iOS的新手。我尝试了更多时间进行搜索,但结果并非我所愿。首先,我的自定义单元格有5行。单元格仅包含文本,我想在包含一个图像的单元格的任何索引处添加一行。那么我们如何实施呢?

最佳答案

通过评论,我知道您对UITableView还是很陌生。因此,请阅读一些给定的教程。您将能够在UITableView中添加,删除编辑单元格

UITableView tutorial
tutorial 2
Table view programming guide

对于简单的插入,请像这样

向视图添加按钮及其操作

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

关于ios - 使用自定义单元格在TableView中的任何索引处插入一行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16183134/

10-12 14:46
查看更多