How to get a UITableView's visible sections?

前端 未结 12 1787
轻奢々
轻奢々 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:29

    Extract the sections from the list of visible rows:

    NSArray *indexPathsForVisibleRows = [tableView indexPathsForVisibleRows];
    NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
    for ( NSIndexPath *indexPath in indexPathsForVisibleRows ) {
         [indexSet addIndex:indexPath.section];
    }
    NSLog(@"indexSet %@",indexSet);
    // indexSet [number of indexes: 5 (in 1 ranges), indexes: (9-13)]
    

    Or:

    NSArray *indexPathsForVisibleRows = [detailTableView indexPathsForVisibleRows];
    NSMutableSet *sectionSet = [NSMutableSet set];
    for ( NSIndexPath *indexPath in indexPathsForVisibleRows ) {
        [sectionSet addObject:[NSNumber numberWithInt:indexPath.section]];
    }
    NSLog(@"sectionSet %@",sectionSet);
    // sectionSet {(13, 11, 9, 10, 12 )}
    

提交回复
热议问题