【问题标题】:UITableView insert row below tapped cellUITableView 在点击的单元格下方插入行
【发布时间】:2014-02-28 22:19:21
【问题描述】:

我非常困惑。我查看了几篇帖子,试图弄清楚如何做到这一点。我想做的是检测何时点击UITableViewCell,然后在该行的正下方插入一行,并带有一个菜单。

我实现了检测被点击的项目,但我不知道如何在刚刚被点击的行下方插入新行。

这是处理点击单元格的方法。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    indexPath = nil;
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    Locations *location = nil;


    if (self.searchDisplayController.active) {
        indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
        location= [self.searchResults objectAtIndex:indexPath.row];

        NSLog(@"location : %@" ,location.locationName);
        [self.locations addObject:@"New Entry"];


        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:YES];

        //[self.tableViewForScreen insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

    } else {
        indexPath = [self.tableView indexPathForSelectedRow];
        location = [self.locations objectAtIndex:indexPath.row];
         NSLog(@"location : %@" ,location.locationName);
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:YES];
    }


    [tableView deselectRowAtIndexPath:indexPath animated:YES];



     // set the tapped item pointer equals to the locations mutable array variable wich essentially is the
    //  data source of the table view.
    //Locations *tappedItem =[self.locations objectAtIndex:indexPath.row];

    //access the location name and print to log.



}

显然它在插入部分失败

[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:YES];

有人可以帮帮我吗?

错误如下。

原因:'无效更新:第 0 节中的行数无效。 更新后现有部分中包含的行数 (154) 必须等于该节之前包含的行数 更新(154),加上或减去插入或删除的行数 从该部分(插入 1 个,删除 0 个)并加上或减去数字 移入或移出该部分的行数(0 移入,0 移出)。'

这是我看过的帖子。 UITableView action row http://adcdownload.apple.com//videos/wwdc_2011__sd/session_125__uitableview_changes_tips_tricks.m4v

【问题讨论】:

    标签: objective-c uitableview ios7


    【解决方案1】:

    问题是您调用了insertRowsAtIndexPath:,但您没有首先更新数据源以包含新行的数据。

    在搜索表而不是主表中选择一行时,您似乎正确地执行了此操作。

    【讨论】:

    • 感谢 maddy,虽然我有点这样,但我不是在这里这样做[self.locations addObject:@"New Entry"];
    • 但这是针对搜索表的。您不会对主表执行此操作(在您的 if 语句的 else 部分内)。
    • 谢谢你,这正是问题所在。如果你不介意我喜欢在点击的单元格之后插入一个自定义单元格,还有一个问题。现在我只是拿起当前单元格的索引路径我怎么说像索引路径 + 1?
    • 新建indexPath:NSIndexPath *newPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];.
    【解决方案2】:

    首先不要使用

    indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
    

    您可以从方法参数中获取 indexPath

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    

    您似乎遇到了问题,因为您正在调用insertRowsAtIndexPaths,但您没有更新数据源。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多