How to get a UITableView's visible sections?

前端 未结 12 1788
轻奢々
轻奢々 2020-12-14 19:24

UITableView provides the methods indexPathsForVisibleRows and visibleCells, but how can I get the visible sections?

12条回答
  •  再見小時候
    2020-12-14 20:22

    Swift 4 and Swift 5

    extension UITableView {
    
        func returnVisibleCellsIndexes() -> [IndexPath] {
            var indexes = [IndexPath]()
            for cell in self.visibleCells {
                if let indexpath = self.returnIndexPath(cell: cell) {
                    indexes.append(indexpath)
                }
            }
            return indexes
        }
        func returnIndexPath(cell: UITableViewCell) -> IndexPath?{
            guard let indexPath = self.indexPath(for: cell) else {
                return nil
            }
            return indexPath
        }
        
        func returnVisibleCells() -> [UITableViewCell]{
            let visiblecell = self.visibleCells
            return visiblecell
        }
        
    }
    

    How to use

      
        let visibleCellsIndexes = self.tableView.returnVisibleCellsIndexes()
        visibleCellsIndexes.forEach { (indexpath) in
    
                print(indexpath.section)
    
         }
    

提交回复
热议问题