Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get UITableView from UITableViewCell?

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?

like image 797
sinθ Avatar asked Mar 29 '13 21:03

sinθ


People also ask

How do I get IndexPath from cell Swift?

add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.

What is UITableView in Swift?

A view that presents data using rows in a single column. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.1+ tvOS 9.0+


1 Answers

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

Objective-C

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

Swift

var view = cellInstance.superview while (view != nil && (view as? UITableView) == nil) {   view = view?.superview }          if let tableView = view as? UITableView {    tableView.beginUpdates()    tableView.endUpdates() } 
like image 68
Muhammad Idris Avatar answered Oct 01 '22 06:10

Muhammad Idris