How to find out how much UITableViewCell is visible

UITableView has methods for determining which cells are currently visible. What I'm trying to figure out is how many cells are visible.

For example, as you drag the table down, the โ€œnewly visibleโ€ cell at the top of the table does not appear, but a row (pixel) appears at a time until the entire cell is visible. How can I determine how much of this cell is visible at any time when a table tab is being dragged?

My ultimate goal is for the user to drag and drop the table to change the displayed view in the cell, depending on how visible it is at any given time.

Any suggestions?

+4
source share
5 answers

I have not tested it, but I would try something like:

UITableViewCell *cell; UIView *parent = cell.superview; CGRect overlap = CGRectIntersection(cell.frame, parent.bounds); 

then compare specific rectangles.

+4
source

You can try something like this:

 -(void)scrollViewDidScroll:(UIScrollView *)sender { [self checkWhichVideoToEnable]; } -(void)checkWhichVideoToEnable { for(UITableViewCell *cell in [tblMessages visibleCells]) { if([cell isKindOfClass:[VideoMessageCell class]]) { NSIndexPath *indexPath = [tblMessages indexPathForCell:cell]; CGRect cellRect = [tblMessages rectForRowAtIndexPath:indexPath]; UIView *superview = tblMessages.superview; CGRect convertedRect=[tblMessages convertRect:cellRect toView:superview]; CGRect intersect = CGRectIntersection(tblMessages.frame, convertedRect); float visibleHeight = CGRectGetHeight(intersect); if(visibleHeight>VIDEO_CELL_SIZE*0.6) // only if 60% of the cell is visible { // unmute the video if we can see at least half of the cell [((VideoMessageCell*)cell) muteVideo:!btnMuteVideos.selected]; } else { // mute the other video cells that are not visible [((VideoMessageCell*)cell) muteVideo:YES]; } } } } 
+4
source

See Vadim Elagin's answer here: fooobar.com/questions/74489 / ... , which explains how to convert cells to the coordinates of the view of the parent table and determine whether it is fully visible or not ..

+2
source

I used the advice of bshirley and some others to do something like this. Works like a charm for my things that only show a couple of cells at a time. Some change may be required if you want to use this with more cells.

  - (void) scrollViewDidEndDecelerating:(UITableView *)scrollView { NSArray *visibleCellIndexPaths = TableView.indexPathsForVisibleRows; NSIndexPath *indexPathLow = [visibleCellIndexPaths valueForKeyPath:@"@min.self"]; NSIndexPath *indexPathHigh = [visibleCellIndexPaths valueForKeyPath:@"@max.self"]; NSArray *cells = TableView.visibleCells; UITableViewCell *cell1 = [cells objectAtIndex:0]; UIView *parent1 = cell1.superview; CGRect overlap1 = CGRectIntersection(cell1.frame, parent1.bounds); UITableViewCell *cell2 = [cells objectAtIndex:1]; UIView *parent2 = cell2.superview; CGRect overlap2 = CGRectIntersection(cell2.frame, parent2.bounds); if (overlap1.size.height > overlap2.size.height) { [TableView scrollToRowAtIndexPath:indexPathLow atScrollPosition:UITableViewScrollPositionTop animated:YES]; } else { [TableView scrollToRowAtIndexPath:indexPathHigh atScrollPosition:UITableViewScrollPositionTop animated:YES]; } 

}

+2
source

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 .

+2
source

Source: https://habr.com/ru/post/1397718/


All Articles