Below is a variant of the one described above in Swift:
var cellRect = tableView.rectForRowAtIndexPath(indexPath) if let superview = tableView.superview { let convertedRect = tableView.convertRect(cellRect, toView:superview) let intersect = CGRectIntersection(tableView.frame, convertedRect) let visibleHeight = CGRectGetHeight(intersect) }
visibleHeight is part of the visible cell. Another step can be added to calculate the relationship - between zero and one - of the visible cell:
var cellRect = tableView.rectForRowAtIndexPath(indexPath) if let superview = tableView.superview { let convertedRect = tableView.convertRect(cellRect, toView:superview) let intersect = CGRectIntersection(tableView.frame, convertedRect) let visibleHeight = CGRectGetHeight(intersect) let cellHeight = CGRectGetHeight(cellRect) let ratio = visibleHeight / cellHeight }
To change visibility depending on visibility - as indicated above, this code must be included in the table view of the UIScrollView delegate of the superclass, the UIScrollViewDelegate method scrollViewDidScroll .
However, this will only affect cells when scrolling through them. Cells that are already visible will not be affected. For those, the same code should be used in the UITableViewDelegate method didEndDisplayingCell .
source share