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

前端 未结 5 2030
执笔经年
执笔经年 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:40

    You can use: [self.tableView visibleCells]

    That will return a nsarray of UITableViewCell objects, each representing a visible cell in the receiving table view.

    http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITableView_Class/Reference/Reference.html

    0 讨论(0)
  • 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)
    
    }
    
    0 讨论(0)
  • 2021-01-02 05:47
    [self.myTableView visibleCells];
    
    NSArray *anArrayOfIndexPath = [NSArray arrayWithArray:[self.myTableView indexPathsForVisibleRows]];
    
    NSIndexPath *indexPath= [anArrayOfIndexPath lastObject];
    
    

    Hope this will help you.

    0 讨论(0)
  • 2021-01-02 05:48

    You can use tableView indexPathsForVisibleRows function, which returns an NSArray of NSIndexPath for all visible UITableViewCell. From indexPath.row you can find your array index.

    NSArray *visible       = [tableView indexPathsForVisibleRows];
    NSIndexPath *indexpath = (NSIndexPath*)[visible objectAtIndex:0];
    

    indexPath.row will give you index of first object displayed.

    0 讨论(0)
  • 2021-01-02 05:50

    you can use indexPathsForVisibleRows

    Returns an array of index paths each identifying a visible row in the receiver.

    - (NSArray *)indexPathsForVisibleRows
    

    Return Value

    An array of NSIndexPath objects each representing a row index and section index that together identify a visible row in the table view. Returns nil if no rows are visible.

    0 讨论(0)
提交回复
热议问题