【问题标题】:IOS swift can I get the current visible view from inside the TableViewCellIOS swift我可以从TableViewCell中获取当前可见视图吗
【发布时间】:2017-11-09 04:59:35
【问题描述】:

我有一个 TableView 单元格,其中一些单元格上有视频,这些视频设置为在我滚动时自动播放,并且一个单元格有一个可以正确播放的视频,我知道当前索引行工作正常。我想知道的是如何从 UiTableViewCell 中获取当前可见行?这样我就可以在用户滚动时停止视频。这是我的代码

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   let cell = tableView.dequeueReusableCell(withIdentifier: "MyCellTVC", for: indexPath) as! MyCellTVC
             let movieURL = URL(string: streamsModel.stream_image_string[indexPath.row])

            cell.videoCapHeight.constant = CGFloat(Float(cell.pic_height!))
            cell.playerView = AVPlayer(url: movieURL!)
            streamsModel.MyAVPlayer.player = cell.playerView
            streamsModel.MyAVPlayer.view.frame = cell.videoView.bounds
            cell.videoView.addSubview(streamsModel.MyAVPlayer.view)
           controller.addChildViewController(streamsModel.MyAVPlayer)

           streamsModel.MyAVPlayer.player?.play()
   // I would like to get the current visible view row 
   // here so I can do some logic
   return cell

  }

我想在那里获取行号,因为我想在那里做一些逻辑,因为我可以访问 TableView (cell dequeueReusableCell) 中的 UILabels。

【问题讨论】:

    标签: swift uitableview nsindexpath


    【解决方案1】:

    嗯,很可能表格视图一次会显示多个单元格。 要知道何时显示和删除单元格,您可以实现 2 个委托方法:

    optional func tableView(_ tableView: UITableView, 
                willDisplay cell: UITableViewCell, 
                   forRowAt indexPath: IndexPath)
    
    optional func tableView(_ tableView: UITableView, 
           didEndDisplaying cell: UITableViewCell, 
                   forRowAt indexPath: IndexPath)
    

    【讨论】:

      【解决方案2】:

      您可以使用 tableView indexPathForRow 方法。在scrollViewDidScroll中实现,通过CGPoint获取indexPath。根据您的单元格大小更改 CGPoint。

      extension ViewController : UIScrollViewDelegate
      {
          func scrollViewDidScroll(_ scrollView: UIScrollView)
          {
      
              if let indexPath = tableViewForPoint.indexPathForRow(at: CGPoint(x: scrollView.contentOffset.x, y: scrollView.contentOffset.y))
              {
                  print("Current cell \(indexPath.row)")
              }
      
          }
      }
      

      【讨论】:

        【解决方案3】:

        要获取表格视图的可见单元格,可以使用以下代码。

        guard let visibleCell = self.tableView.visibleCells.first else {
           return
        }
        let view = visibleCell.contentView
        

        visibleCell.contentView 将给出可见单元格的视图。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-04-15
          • 1970-01-01
          • 2014-12-22
          • 1970-01-01
          • 1970-01-01
          • 2017-08-24
          相关资源
          最近更新 更多