Getting visible cell from UITableView pagingEnabled

前端 未结 5 1887
孤城傲影
孤城傲影 2020-12-13 15:35

I have a UITableView with pagingEnabled. Each cell takes up the viewing area of the table. Meaning, each cell is the same height and width as the table. I\'m using custom

相关标签:
5条回答
  • 2020-12-13 15:54

    Rather than focusing on when the UITableView requests a cell, you should be focusing on when it displays the cell, which is indicated by the delegate method tableView:willDisplayCell:forRowAtIndexPath.

    0 讨论(0)
  • 2020-12-13 16:03

    Swift 4+

        
        func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
            let firstVisibleIndexPath = self.tableView.indexPathsForVisibleRows?[0]
            
            print("top visible cell section  is \([firstVisibleIndexPath!.section])")
        }
    
    0 讨论(0)
  • 2020-12-13 16:13

    Converted Douglas example to Swift:

    let tableView = self.tableView // Or however you get your table view
    let paths = tableView.indexPathsForVisibleRows
    
    //  For getting the cells themselves
    let visibleCells : NSMutableSet = []
    
    for path in paths! {
        visibleCells.addObject(tableView.cellForRowAtIndexPath(path)!)
    }
    
    0 讨论(0)
  • 2020-12-13 16:13

    Simple and elegant way to retrieve visible cells of UITableView, no need to get visible cells by using indexpath values

    NSArray * visibleCells = tableView.visibleCells;
    NSLog(@"Total visible Cells: %i", [visibleCells count]);
    

    If indexpath's of visible cells are needed

    NSArray * paths = [tableView indexPathsForVisibleRows];
    
    0 讨论(0)
  • 2020-12-13 16:14

    Well, on the off chance that you never figured out a solution, or for whoever comes to this question next, I'll provide you with the answer you were looking for. UITableView will provide you with the indexPaths you are looking for, and then UITableView will happily provide you with the cells that match those index paths:

    UITableView *tableView = self.tableView; // Or however you get your table view
    NSArray *paths = [tableView indexPathsForVisibleRows];
    
    //  For getting the cells themselves
    NSMutableSet *visibleCells = [[NSMutableSet alloc] init];
    
    for (NSIndexPath *path in paths) {
        [visibleCells addObject:[tableView cellForRowAtIndexPath:path]];
    }
    
    // Now visibleCells contains all of the cells you care about.
    
    0 讨论(0)
提交回复
热议问题