2014-02-28 49 views
0

我非常困惑。我看了几篇文章,试图找出如何做到这一点。我想要做的是检测什么时候点击UITableViewCell,然后在菜单的右下方插入一行。UITableView插入单元格下方的行

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

以下是处理分接单元的方法。

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

回答

1

的问题是,你叫insertRowsAtIndexPath:但你不先更新数据源,包括新行的数据。

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

+0

感谢马迪我虽然那样,但我没有这样做'[self.locations addObject:@“New Entry”];' – Miguel

+0

但是这是搜索表。你不这样做主表(在你的'if'语句的'else'部分内)。 – rmaddy

+0

谢谢你maddy,这正是问题所在。还有一个问题,如果你不介意我喜欢在轻敲单元格后面插入一个自定义单元格。现在我只是拿起当前单元格的索引路径,我怎么能说像indexpath + 1? – Miguel

1

首先不使用

indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow]; 

你可以采取indexPath从方法参数

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

看来你有问题,因为你在呼唤insertRowsAtIndexPaths,但你没有更新的数据源。

相关问题