Best Way to Check If Uitableviewcell Is Completely Visible

Best way to check if UITableViewCell is completely visible

You can get the rect of a cell with rectForRowAtIndexPath: method and compare it with tableview's bounds rect using CGRectContainsRect function.

Note that this will not instantiate the cell if it is not visible, and thus will be rather fast.

Swift

let cellRect = tableView.rectForRowAtIndexPath(indexPath)
let completelyVisible = tableView.bounds.contains(cellRect)

Obj-C

CGRect cellRect = [tableView rectForRowAtIndexPath:indexPath];
BOOL completelyVisible = CGRectContainsRect(tableView.bounds, cellRect);

Of course this will not regard the table view being clipped by a superview or obscured by another view.

Check if a UITableViewCell is completely visible

As you have, get the visible cells. The only ones that might be partial are the top and bottom ones. For each, check if its rect (rectForRowAtIndexPath:) is fully within the bounds of the table view (based on contentSize and contentOffset, using CGRectContainsRect).

Check if a specific UITableViewCell is visible in a UITableView

Note that you can as well use indexPathsForVisibleRows this way:

    NSUInteger index = [_people indexOfObject:person];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
if ([self.tableView.indexPathsForVisibleRows containsObject:indexPath]) {
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}

If you have the indexPath (and don't need the actual Cell) it might be cheaper.

PS: _people is the NSArray used as my backend in this case.

Determine if image in table view cell is completely visible

Assuming you have set up view controller that manages image cells, here is a part of code for such controller, which is delegate of UITableView, that demos an approach of how to track becoming images visible. Hope it will be helpful.

@IBOutlet weak var tableView: UITableView!

override func viewDidAppear(_ animated: Bool) {
self.verifyImagesVisibility()
}

func scrollViewDidScroll(_ scrollView: UIScrollView) {
self.verifyImagesVisibility()
}

private func verifyImagesVisibility() {
for cell in self.tableView.visibleCells {
if let imageView = cell.imageView {
let visibleRect = self.tableView.bounds
let imageRect = imageView.convert(imageView.bounds, to: tableView)

imageView.layer.borderWidth = 4.0 // for test
if visibleRect.contains(imageRect) {
// do anything here for image completely shown
imageView.layer.borderColor = UIColor.red.cgColor // for test
} else {
// do anything here for image become partially hidden
imageView.layer.borderColor = UIColor.black.cgColor // for test
}
}
}
}

Determine if a tableview cell is visible

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.



Related Topics



Leave a reply



Submit