UITableView在tapped cell下方插入行

时间:2014-02-28 22:19:22

标签: objective-c uitableview ios7

我非常困惑。我看过几个帖子试图弄清楚如何做到这一点。我想要做的是检测点击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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

首先不要使用

indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];

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

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

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

相关问题