2

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

4

3 回答 3

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

 }

编辑

我想你需要这样的东西

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

于 2013-04-24T05:02:51.497 回答
1

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

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

给出适当的indexPaths和行的动画。

于 2013-04-24T04:54:00.067 回答
0

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

于 2013-04-24T04:04:18.493 回答