tableView:indexPathForCell返回nil [英] tableView:indexPathForCell returns nil

查看:199
本文介绍了tableView:indexPathForCell返回nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用方法 tableView:indexPathForCell 来实现一个自定义委托,可以动态调整 UITableViewCell 的基础内部的 UIWebView 的帧大小。问题是当我试图找出特定单元格的indexPath是什么时, tableView:indexPathForCell 返回 nil

I am using the method tableView:indexPathForCell to implement a custom delegate that can dynamically resize a UITableViewCell based on the frame size of the UIWebView that is inside of it. The problem is that tableView:indexPathForCell is returning nil when I try to find out what the indexPath of a particular cell is:

- (void)trialSummaryTableViewCell:(TrialSummaryTableViewCell *)cell shouldAssignHeight:(CGFloat)newHeight {
    NSIndexPath *indexPath = [tableV indexPathForCell:cell];
    NSLog(@"tableV: %@\ncell: %@\nindexPath: %@", tableV, cell, indexPath); //<-- 
    // ...
}

这里, tableV 不返回 nil ,单元格不返回 nil ,但是 indexPath 返回 nil

Here, tableV does not return nil, cell does not return nil, but indexPath returns nil.

什么是我做错了吗?

编辑:我从调用 - (void)trialSummaryTableViewCell tableView:cellForRowAtIndexPath:方法

I am calling -(void)trialSummaryTableViewCell from the tableView:cellForRowAtIndexPath: method

推荐答案

此时此单元格可能不可见。 tableView:indexPathForCell在这种情况下返回nil。我使用indexPathForRowAtPoint解决了这个问题,即使单元格不可见,此方法也能正常工作。代码:

It could be that the cell is not visible at this moment. tableView:indexPathForCell returns nil in this situation. I solved this using indexPathForRowAtPoint this method works even if the cell is not visible. The code:

UITableViewCell *cell = textField.superview.superview;
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:cell.center];

这篇关于tableView:indexPathForCell返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆