Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if a tableview cell is visible

Is there any way to know if a tableview cell is currently visible? I have a tableview whose first cell(0) is a uisearchbar. If a search is not active, then hide cell 0 via an offset. When the table only has a few rows, the row 0 is visible. How to determine if row 0 is visible or is the top row?

like image 429
Jim B Avatar asked Jul 24 '10 19:07

Jim B


People also ask

How do I hide a Tableview section?

You can't "hide" a section as such, but you can "delete" it from the table view using the deleteSections:withRowAnimation: method. This will remove it from the view, with an optional animation, without affecting your backing data. (You should, however, update the data anyway so that the section doesn't reappear.)

How does a Tableview work?

Overview. Table views in iOS display rows of vertically scrolling content in a single column. Each row in the table contains one piece of your app's content. For example, the Contacts app displays the name of each contact in a separate row, and the main page of the Settings app displays the available groups of settings ...


2 Answers

UITableView has an instance method called indexPathsForVisibleRows that will return an NSArray of NSIndexPath objects for each row in the table which are currently visible. You could check this method with whatever frequency you need to and check for the proper row. For instance, if tableView is a reference to your table, the following method would tell you whether or not row 0 is on screen:

-(BOOL)isRowZeroVisible {   NSArray *indexes = [tableView indexPathsForVisibleRows];   for (NSIndexPath *index in indexes) {     if (index.row == 0) {       return YES;     }   }    return NO; } 

Because the UITableView method returns the NSIndexPath, you can just as easily extend this to look for sections, or row/section combinations.

This is more useful to you than the visibleCells method, which returns an array of table cell objects. Table cell objects get recycled, so in large tables they will ultimately have no simple correlation back to your data source.

like image 81
devunwired Avatar answered Oct 06 '22 15:10

devunwired


To checking tableview cell is visible or not use this code of line

    if(![tableView.indexPathsForVisibleRows containsObject:newIndexPath])     {        // your code      } 

here newIndexPath is IndexPath of checking cell.....

Swift 3.0

if !(tableView.indexPathsForVisibleRows?.contains(newIndexPath)) {   // Your code here  } 
like image 30
Anand Mishra Avatar answered Oct 06 '22 16:10

Anand Mishra