Determine if a tableview cell is visible

IphoneUitableview

Iphone Problem Overview


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?

Iphone Solutions


Solution 1 - Iphone

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.

Solution 2 - Iphone

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

}

Solution 3 - Iphone

I use this in Swift 3.0

extension UITableView {

    /// Check if cell at the specific section and row is visible
    /// - Parameters:
    /// - section: an Int reprenseting a UITableView section
    /// - row: and Int representing a UITableView row
    /// - Returns: True if cell at section and row is visible, False otherwise
    func isCellVisible(section:Int, row: Int) -> Bool {
        guard let indexes = self.indexPathsForVisibleRows else {
            return false
        }
        return indexes.contains {$0.section == section && $0.row == row }
    }  }

Solution 4 - Iphone

Swift Version:

if let indices = tableView.indexPathsForVisibleRows {
    for index in indices {
        if index.row == 0 {
            return true
        }
    }
}
return false

Solution 5 - Iphone

Another solution (which can also be used to check if a row is fully visible) would be to check if the frame for the row is inside the visible rect of the tableview.

In the following code, ip represents the NSIndexPath:

CGRect cellFrame = [tableView rectForRowAtIndexPath:ip];
if (cellFrame.origin.y<tableView.contentOffset.y) { // the row is above visible rect
  [tableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:NO];
}
else if(cellFrame.origin.y+cellFrame.size.height>tableView.contentOffset.y+tableView.frame.size.height-tableView.contentInset.top-tableView.contentInset.bottom){ // the row is below visible rect
  [tableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionBottom animated:NO];
}

Also using cellForRowAtIndexPath: should work, since it returns a nil object if the row is not visible:

if([tableView cellForRowAtIndexPath:ip]==nil){
  // row is not visible
}

Solution 6 - Iphone

Note that "Somewhat visible" is "visible". Also, in viewWillAppear, you might get false positives from indexPathsForVisibleRows as layout is in progress, and if you're looking at the last row, even calling layoutIfNeeded() won't help you for tables. You'll want to check things in/after viewDidAppear.

This swift 3.1 code will disable scroll if the last row is fully visible. Call it in/after viewDidAppear.

    let numRows = tableView.numberOfRows(inSection: 0) // this can't be in viewWillAppear because the table's frame isn't set to proper height even after layoutIfNeeded()
    let lastRowRect = tableView.rectForRow(at: IndexPath.init(row: numRows-1, section: 0))
    if lastRowRect.origin.y + lastRowRect.size.height > tableView.frame.size.height {
        tableView.isScrollEnabled = true
    } else {
        tableView.isScrollEnabled = false
    }

Solution 7 - Iphone

Swift:

Improved @Emil answer

extension UITableView {
    
    func isCellVisible(indexPath: IndexPath) -> Bool {
        guard let indexes = self.indexPathsForVisibleRows else {
            return false
        }
        return indexes.contains(indexPath)
    }
}

Solution 8 - Iphone

IOS 4:

NSArray *cellsVisible = [tableView indexPathsForVisibleRows];
NSUInteger idx = NSNotFound;
idx = [cellsVisible indexOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop)
{
    return ([(NSIndexPath *)obj compare:indexPath] == NSOrderedSame);
}];

if (idx == NSNotFound)
{
 

Solution 9 - Iphone

Best to check by Frame

let rectOfCellInSuperview = yourTblView.convert(yourCell.frame, to: yourTblView.superview!) //make sure your tableview has Superview
if !yourTblView.frame.contains(rectOfCellInSuperview) {
       //Your cell is not visible
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJim BView Question on Stackoverflow
Solution 1 - IphonedevunwiredView Answer on Stackoverflow
Solution 2 - IphoneAnand MishraView Answer on Stackoverflow
Solution 3 - IphoneEmilView Answer on Stackoverflow
Solution 4 - Iphoneems305View Answer on Stackoverflow
Solution 5 - Iphonealex-iView Answer on Stackoverflow
Solution 6 - IphonexaphodView Answer on Stackoverflow
Solution 7 - IphoneJuan BoeroView Answer on Stackoverflow
Solution 8 - IphoneCSmithView Answer on Stackoverflow
Solution 9 - IphoneiOS LifeeView Answer on Stackoverflow