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?
easiest way would be to use a framework like jquery and use for instance
// to check if it is visible
if ($("rowidhere").is(":visible")) { // true }
// to check if it's first row
if ($("#tableidhere").children(’:first-child’) == $("rowidhere")) { // true }
UITableView
has an instance method called indexPathsForVisibleRows
that will return an NSArray
of NSIndexPath
objects for each row in the table 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.