如何从该表内的自定义单元格访问其中有 UITableView 的 UIView。我找不到方法来做到这一点。 谢谢
最佳答案
您可以添加一个指向 UITableView 的实例变量,并在创建/配置单元格时设置它(例如在 tableView:cellForRowAtIndexPath: 中)。确保您的单元格不保留 tableView。 知道你的单元格的tableView,调用[parentTableView superView]来访问UITableView的父 View :
@interface PropertyListingCell : UITableViewCell {
__weak id parentTableView;
}
- (void) setParentTableView:(UITableView*)tv; // parentTableView = tv;
在 UITableViewController 实现中:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//dequeue/create and configure your custom cell here
[cell setParentTableView:tableView];
return cell;
}
更新:
如果您使用最新的 XCode(至少 4.3),您只需添加
@property (weak) UITableView *parentTableView; // use unsafe_unretained instead of weak if you're targeting iOS 4.x
到 UITableViewCell 子类的 @interface
部分。然后,当您创建单元格(在 tableView:cellForRowAtIndexPath:
中)时,相应地设置此属性:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// ...
//dequeue/create and configure your custom cell here
// ...
cell.parentTableView = tableView;
return cell;
}
并在您的单元格类中调用self.parentTableView
来访问该单元格所属的tableView。
关于iphone - 从自定义单元格访问父 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5101555/