【问题标题】:UITableView - insert new row by tapping on existing cellUITableView - 通过点击现有单元格插入新行
【发布时间】:2014-07-31 12:29:49
【问题描述】:

我将UITableView 与原型单元格一起使用,并希望最上面的单元格成为“加法器”单元格:当用户点击该单元格时,将执行转场。由于UITableView 是动态生成的(顶部除外)并且单元格正在被重用,我有两个问题:

1 - 是否可以制作一个static 单元,而所有其他单元仍将保留原型单元?

2 - 如果没有,如何实施此解决方案?我认为,它必须在

中实现
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

但我不知道,这种方法是否合适。我是否正确,要实现这个自定义单元格,我需要检查是否indexPath.row = 0 并用自定义替换所需的单元格,同时以编程方式将所有单元格向下移动一行?

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    Yiu 可以在你的单元格的 Tap 上做,你可以创建两个不同的单元格,一个是你的原型单元格,一个是你的详细信息

    在你的

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        if (indexPath.row == 0) {
            AdderCell *cell = (AdderCell*)[tableView dequeueReusableCellWithIdentifier:@"AdderCell" forIndexPath:indexPath];
         } else {
           CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:@"CustomCell" forIndexPath:indexPath];
         }
    }
    

    中点击您的 Adder 单元格
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        if (indexPath.row == 0) {
                    // add data to your row array and reload the tableRow
                 } else {
                   // do your tap action
                 }
         }
    

    【讨论】:

    • 感谢您的解释。几位cmets:首先,你有点误会我了。自定义单元格只会使用参数执行 segue,因此无需重新加载任何内容。其次,我有一个关于- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 的问题——所有的TableView 都附加到CoreData 和FetchedResultsController。 FRC 给出的索引路径为 0 - CDObject0, 1 - CDObj1... 但是如果 CD 中没有对象,它不会调用方法 tableViewcellForRowAtIndexPath。我需要在 viewDidLoad 上手动调用它吗?
    • 我是否需要重写一些方法来制作 indexPath: 0 - custom, 1- CDObj0, 2 - CDObj1?
    • 如果您使用的是 FetchedResultsController,那么如果您已经实现了它的委托,它将自动重新加载表!
    • 是的,但是 CoreData 转移怎么样?我需要这样做吗,还是会自动完成?
    • 是的,在某种程度上!在这里查看更多stackoverflow.com/questions/9322885/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    相关资源
    最近更新 更多