Get Cell position in UITableview

前端 未结 3 1410
深忆病人
深忆病人 2020-12-07 20:27

I am trying to get position of cell in tableview. from this code i am getting cell position

int numberOfSections = [UITableView numberOfSections];
int number         


        
相关标签:
3条回答
  • 2020-12-07 21:04

    I did it like this:

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    

    That was all I had to do, than I could all cell.frame, cell.bounds, etc.

    0 讨论(0)
  • 2020-12-07 21:08

    NOTE:I hope, question might have updated again, please use the other answers that may applicable to you.

    You can use the visibleCells method which will return the currently visible cells in your tableview:

    NSArray *visibleCellsList=[tableview visibleCells];
    
    for (UITableViewCell* currentCell in visibleCellsList) {
    
        NSLog(@"cell : %@ \n\n",currentCell.textLabel.text);
    
    }
    
    0 讨论(0)
  • 2020-12-07 21:16

    To get the position of a UITableViewCell relative to the view, in which it's located (e.g. relative to your visible area):


    Swift 5 (and also Swift 4, Swift 3):

    let rectOfCellInTableView = tableView.rectForRow(at: indexPath)
    let rectOfCellInSuperview = tableView.convert(rectOfCellInTableView, to: tableView.superview)
        
    print("Y of Cell is: \(rectOfCellInSuperview.origin.y)")
    

    Objective-C:

    CGRect rectOfCellInTableView = [tableView rectForRowAtIndexPath: indexPath];
    CGRect rectOfCellInSuperview = [tableView convertRect: rectOfCellInTableView toView: tableView.superview];
    
    NSLog(@"Y of Cell is: %f", rectOfCellInSuperview.origin.y);
    

    Swift 2 (really?):

    let rectOfCellInTableView = tableView.rectForRowAtIndexPath(indexPath)
    let rectOfCellInSuperview = tableView.convertRect(rectOfCellInTableView, toView: tableView.superview)
    
    print("Y of Cell is: \(rectOfCellInSuperview.origin.y)")
    
    0 讨论(0)
提交回复
热议问题