How to get indexPath.row of tableView which is currently being displayed?

前端 未结 5 2029
执笔经年
执笔经年 2021-01-02 05:19

I have a tableView with many values.

I want to get the first indexPath.row value of the table which is being displayed currently.

How can I do

5条回答
  •  迷失自我
    2021-01-02 05:45

    Get exact index number at center currently being displayed:

     let middleIndex = ((tableView.indexPathsForVisibleRows?.first?.row)! + (tableView.indexPathsForVisibleRows?.last?.row)!)/2
    

    Example:

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    
      let middleIndex = ((tableView.indexPathsForVisibleRows?.first?.row)! + (tableView.indexPathsForVisibleRows?.last?.row)!)/2
      print(middleIndex)
    
    }
    

提交回复
热议问题