iphone - 从自定义单元格访问父 View

标签 iphone uitableview uiview

如何从该表内的自定义单元格访问其中有 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/

相关文章:

iphone - 如何在 iOS 中按修改日期对文件进行排序

iphone - 在 XCode 4 中构建静态库奇怪的错误

ios - 从其他 ViewController 中删除带有标签的 View

ios - 不同 View 的 CAGradientLayer

ios - 在我的 UITableView 中选择一个单元格时,选择似乎是随机的

iOS 自定义 UIView 在 iphone xr 和 iphone 7 中具有不同的布局

objective-c - uiview 的自动调整大小

iphone - 如何在没有 objective-c 代码的情况下显示 ios 应用程序版本号

ios - 如何从搜索结果 DetailView 返回 UITableView

Swift 如何为用户使用 Firebase 投票添加 5 星级评分