无法获取UITableViewCell的索引路径

时间:2017-01-04 06:33:07

标签: ios objective-c uitableview

我有一个表格视图,其中动态单元格由autolayout构成。我在UITableViewCell中有一个UITextView作为子视图。这个UITextView可以垂直增长,即对于此文本视图禁用滚动,并且它的高度约束等于单元格内容视图的高度。因此,每当文本视图高度增加时,相应的单元格高度也会增加。

我需要得到任何给定单元格的索引路径。 当textview高度高于屏幕高度时,indexPathForCell返回nil。我也试过indexPathForRowAtPoint:[cell center]它也返回nil。我无法从我的模型单元格数组中获取indexpath,因为模型没有我的单元格(单元格已从模型数组中删除)。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      NSMutableArray *cellArray = [tableViewCellsDictionary objectForKey:[NSNumber numberWithUnsignedLong:sectionIndex]];
      return [cellArray objectAtIndex:rowIndex];
  }

-(void) updateTextViewTextInCell:(CustomCell *) cell {
      NSIndexPath *currentCellIndexPath1 = [tableView indexPathForCell:cell];  // Returns nil
      NSIndexPath *currentCellIndexPath2 = [tableView indexPathForRowAtPoint:[cell center]]; // Returns nil
}

我从updateCellTextView调用此textViewDidEndEditing方法,将textview的文本保存在我的模型中。

但是我无法获得单元格的索引路径。

如何在这种情况下获取单元格的索引路径,即单元格完全在屏幕外时。

Image

2 个答案:

答案 0 :(得分:1)

如果您想获取indexpath中的textViewDidEndEditing个单元格,可以使用此代码。

 CGPoint point = [textView convertPoint:CGPointZero toView:self.tableview];
 NSIndexPath *indexPath = [self.tableview indexPathForRowAtPoint:point];

答案 1 :(得分:0)

显然,你永远不会得到indexpath这样的单元格,因为这只适用于UItableViewDelegates and UItableViewDatasource methods

根据我的理解,现在您需要知道哪个UITextView用户结束发短信。因此,更好的方法之一如下:

  1. in cellForRow
  2. cell.textView.tag = indexPath.row

    您已在此处为textView分配了与其indexpath.row相同的标记。现在,当您谈论textView.tag时,它会为您提供该特定单元格的textViewtextView.tag就是您拥有的特定单元格TextView

    1. - (BOOL)textViewShouldBeginEditing:(TextView *)textView

      textView.tag // gives you indexPath.row of the textview where user ends editing
      
    2. 注意:如果UITextView/UItextfield中有UItableView,则当您的手机信号不在屏幕时,其值始终会丢失。因此,为了解决这个问题,请使用字典保存UITextView/UItextfieldTextdidChange Method的每个值。

      希望你现在明白了。