How to find out if UITableViewCell currently exists?

Given the indexPath UITableViewCell, I want to know if this cell currently exists.

I know how to check this on the screen (use tableView.indexPathsForVisibleRows ). However, I also want to know if it is from the screen, but has already been created (assuming the user has scrolled, but he has not quite entered the screen yet).

How to do it?

+4
source share
6 answers

If you reuse cells with dequeueReusableCellWithIdentifier: tableView will act as a pipeline. A camera scrolling from the screen will immediately go to the cell that will enter the screen. Theoretically, tableView will not create cells anymore than the screen is for you. So it’s pointless that you want to know about a cell from the screen.

UITableViewCell is just a view on the MVC Pattern . This is your model, your data, which determines what should be present in a particular cell at a specific indexPath. Your code will probably look like this:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // dequeue a cell // ... if (!cell) { // init a cell // ... } // for a given indexPath, decide what should be presented in the cell, something like updating the cell properties cell.textLabel.text = [self.data dataShouldBePresentedAtIndexPath:indexPath]; } 
+2
source

You can do

 [self.tableView cellForRowAtIndexPath:indexPath]; 

(not to be confused with the data source method [self tableView:cellForRowAtIndexPath:] ), and if the cell for this pointer path exists, it will be returned. Otherwise, you will get nil .

You definitely do not want your background update process to contain a direct link to the cell, because, as you said, it can scroll from the screen and be processed by the time the selection is complete. Instead, keep a reference to the pointer path or a piece of data that you can use to find the index path, and then use this pointer path with the method above to retrieve the cell.

+9
source

The only existing cells are those that are currently visible. As soon as the cell scrolls outside the view, the cell object becomes available for the UITableViewCell dequeueReusableCellWithIdentifier method, which is stored in OS memory when working with a huge amount of tabular data that will be displayed in such a small amount of real estate.

If you want to keep track of which cells have already been viewed, you must change the underlying objects to have some BOOL or the value set when the object data is passed to the table view cell in your application " cellForRowAtIndexPath: ".

+2
source
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // dequeue a cell // ... if (cell == nill) { // init a cell // ... } // for a given indexPath, decide what should be presented in the cell, something like updating the cell properties cell.textLabel.text = [self.data dataShouldBePresentedAtIndexPath:indexPath]; } 

you can check if the cell exists or not .... hope this works ... :)

0
source

Alternatively you can do something like this

 -(BOOL) isRowPresentInTableView:(int)row withSection:(int)section { if(section < [self.tableView numberOfSections]) { if(row < [self.tableView numberOfRowsInSection:section]) { return YES; } } return NO; } 
0
source

In Swift 3

 let YOURINDEXPATH = IndexPath(row: 4, section: 0) if tableView.cellForRow(at: YOURINDEXPATH) != nil { // DO YOUR STUFFS HERE } 

indicate your index path (YOURINDEXPATH) respectively

0
source

Source: https://habr.com/ru/post/1496377/


All Articles