Home > OS >  Check table view cell fully visible
Check table view cell fully visible

Time:11-24

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

CodePudding user response:

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

CodePudding user response:

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).

  • Related