0

how are you

I need to know how can i determine table view cell fully visible for playing auto play movie in cell and also detect hiding the table view cell.

I have apply this code below in table view but not give me the correct solution.

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt   indexPath: IndexPath) {
   let cellRect = tableView.rectForRow(at: indexPath)
   let isFullyVisible = tableView.bounds.contains(rectInFull)
   if isFullyVisible {
    // Play video
   } 
}

So please can you tell me how can i get the correct table view cell visible

Dark Knight
  • 370
  • 3
  • 13

2 Answers2

1

cell.frame.origin.y >= tableview.contentOffset.y && cell.frame.origin.y + cell.frame.size.height <= tableview.contentOffset.y + tableview.bounds.size.height

Neal.Marlin
  • 494
  • 5
  • 17
  • I found the solution here https://stackoverflow.com/questions/24825994/play-video-on-uitableviewcell-when-it-is-completely-visible – Dark Knight Nov 23 '21 at 19:13
  • @HamzaAlmass Thanks for your link. But I see the key point, the judgement of visibility condition, is the same as what I've pointed out. : ) – Neal.Marlin Nov 24 '21 at 04:23
  • It's not work , you can also check this is best answer from matt https://stackoverflow.com/questions/68108188/determine-percentage-of-visibility-of-horizontal-collectionview-cells-on-screen – Dark Knight Nov 24 '21 at 16:10
  • @HamzaAlmass It doesn’t work, because you check it in a wrong system hook. If in didScroll, It will be fine. – Neal.Marlin Nov 25 '21 at 01:05
  • @HamzaAlmass And of course, you could convert view coordination, that’s your choice. In a table view, cell will not scroll in horizontal axis. So checking vertical axis is fairly enough. See the thing inside, not just the code. – Neal.Marlin Nov 25 '21 at 01:14
  • So you could mark your problem as duplicated. : ) – Neal.Marlin Nov 25 '21 at 01:16
0

You might want to implement the UIScrollViewDelegate method scrollViewDidScroll(_:) and in there you could do the check for visible cells in your table view:

func scrollViewDidScroll(_ scrollView: UIScollView) {
      guard let tv = scrollView as? UITableView else { return }
      
      tv.visibleCells.forEach { $0.playVideo() }
}

Of course assuming that method on the cell (playVideo()) exists and it takes care of being called multiple times when the video is already playing (i.e. must ignore the call if is already playing). Otherwise if you need the indexPaths of visible cells or any fine tuning use either the property indexPathsForVisibleRows or the method indexPathForRows(in:) on the table view, then those index paths you'll optionally obtain inside an array will point to the model's objects (which might implement the play video logic).

valeCocoa
  • 344
  • 1
  • 8
  • I found the solution here I found the solution here stackoverflow.com/questions/24825994/… – Hamza Almass just now Edit – Dark Knight Nov 23 '21 at 19:14
  • 1
    If the cell is partial visible at the edge of the tableview bounds, the cell will also be in the `visibleCells`, I think. – Neal.Marlin Nov 24 '21 at 04:19
  • @Neal.Marlin as I mentioned in my reply there is also the method ‘indexPathsForRows(in:)’ that could be adopted, or eventually ‘visibleCells’ could also be filtered by those having their frame completely contained inside ‘visibleRect’ property of the table view. No matter the detail granularity, I still think the approach of implementing UIScrollViewDelegate’s ‘didScroll’ method is the best suiting this case. – valeCocoa Nov 25 '21 at 00:46