How to get UITableView from UITableViewCell?

sinθ picture sinθ · Mar 29, 2013 · Viewed 66.8k times · Source

I have a UITableViewCell which is linked to an object and I need to tell if the cell is visible. From the research I've done, this means I need to somehow access the UITableView that contains it (from there, there are several ways to check if it's visible). So I'm wondering if UITableViewCell has a pointer to the UITableView, or if there was any other way to get a pointer from the cell?

Answer

Muhammad Idris picture Muhammad Idris · Sep 13, 2013

To avoid checking the iOS version, iteratively walk up the superviews from the cell's view until a UITableView is found:

id view = [tableViewCellInstance superview];

while (view && [view isKindOfClass:[UITableView class]] == NO) {
    view = [view superview]; 
}

UITableView *tableView = (UITableView *)view;