【问题标题】:Check if UITableViewCell is completely visible for automatic playback检查 UITableViewCell 是否完全可见以进行自动播放
【发布时间】:2017-03-16 12:20:15
【问题描述】:

我正在构建一个包含帖子供稿的社交媒体应用。当帖子在屏幕上完全可见时,需要开始播放分配给帖子的曲目。这是我现在的布局。

这是我想出的一段代码,但它只适用于时间线中的第一个帖子。

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    
    let visibleCell = tableView.visibleCells.first as! HomeControllerTableViewCell
    
    
    let firstCellVisibleIndexPath = tableView.indexPathsForVisibleRows?.first
  
    
    if tableView.bounds.contains(tableView.rectForRow(at: firstCellVisibleIndexPath!)){
      let fullyVisibleCell = tableView.cellForRow(at: firstCellVisibleIndexPath!) as! HomeControllerTableViewCell
      
      print(fullyVisibleCell.authorNameLabel.text)
      
    }
    
  }

请帮我弄清楚如何确定哪个单元格是完全可见的。

【问题讨论】:

  • 您只测试第一个:tableView.visibleCells.first。相反,让 allvisibleCells = tableView.visibleCells,然后做一个 for 循环。

标签: ios objective-c swift uitableview uikit


【解决方案1】:

这就是我最终确定哪个单元格在我的布局中完全可见的方式

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    

    
    for item in tableView.indexPathsForVisibleRows!{
      if tableView.bounds.contains(tableView.rectForRow(at: item)){
        let fullyVisibleCell = tableView.cellForRow(at: item) as! HomeControllerTableViewCell
        
       
 
      }
    }
  }

【讨论】:

    【解决方案2】:
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let location = scrollView.panGestureRecognizer.location(in: tablePostView)
        guard let indexPath = tablePostView.indexPathForRow(at: location) else {
            print("could not specify an indexpath")
            return
        }
    
        print("will begin dragging at row \(indexPath.row)")
        let cellRect = tablePostView.rectForRow(at: indexPath)
        let completelyVisible = tablePostView.bounds.contains(cellRect)
        print(completelyVisible)}
    

    【讨论】:

    • 感谢您提交答案!与其只提交代码中的解决方案,不如尝试提供上下文。为什么 OPs 解决方案不起作用?为什么你的工作?有没有 OP 需要理解的概念?
    猜你喜欢
    • 1970-01-01
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-31
    • 1970-01-01
    相关资源
    最近更新 更多