开发者

Reference to the parent tableView of a cell

开发者 https://www.devze.com 2023-02-23 23:22 出处:网络
Is there a way to have a reference of the parent tab开发者_如何转开发leView from the tableview\'s cell?

Is there a way to have a reference of the parent tab开发者_如何转开发leView from the tableview's cell?

Thanks!


You can add this method to your custom subclass of UITableViewCell:

- (id)parentTableView {
    UIView *v = [self superview];
    UIView *previous = nil;
    while (v && ![v isKindOfClass:[UITableView class]] && v != previous) {
        previous = v;
        v = [v superview];
    }
    return v == previous ? nil : v;
}

If you're not subclassing UITableViewCell, just replace self in the code above with your reference to a UITableViewCell.


If you are accessing the cell through the didSelectRowATIndexPath: you can get it easily as

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// here tableView is the one you want.

}
0

精彩评论

暂无评论...
验证码 换一张
取 消