ios - 无法获取 UITableViewCell 的索引路径

标签 ios objective-c uitableview

我有一个表格 View ,其中包含使用 autolayout 构造的动态单元格。我在 UITableViewCell 中有一个 UITextView 作为 subview 。此 UITextView 可以垂直增长,即此 TextView 禁用滚动,并且其高度约束等于单元格内容 View 的高度。因此,每当 textview 高度增加时,相应单元格的高度也会增加。

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

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

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

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

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

Image

最佳答案

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

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

关于ios - 无法获取 UITableViewCell 的索引路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41457601/

相关文章:

ios - Apple 的 doCipher 示例代码中的不良做法 :key:context:padding method

ios - 表格 View 错误

ios - UITableviewcells 侧面不需要的动画

Objective-c - iOS - 隐藏/取消隐藏标签、文本字段等

ios - 没有匹配的函数可调用 'mmul'

ios - GA 事件跟踪中的动态标签名称?

iphone - 我可以使用Xcode 3.1为iOS版本4.0、4.3和5开发应用程序吗

Objective-C 重写方法中的顶部或底部 super 调用

iphone - 2 UIScrollView 中的 UITableView

ios - 在 tableViewCell 中设置字符串数组