UITableView相关整理

257 阅读1分钟

获取滑动到当前区的索引

    //拿到当前滑动到哪个区
    NSArray *arrVisible = self.tableView.indexPathsForVisibleRows;
    NSArray *indexPaths = [arrVisible sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        NSIndexPath *path1 = (NSIndexPath *)obj1;
        NSIndexPath *path2 = (NSIndexPath *)obj2;
        return [path1 compare:path2];
    }];
    
    NSIndexPath *indexPath = (NSIndexPath *)[indexPaths lastObject];
    NSInteger section = indexPath.section;

设置tableView的滑动偏移量

    //点击切换tableView的偏移量----进行展示
    NSInteger index = [self.navButtonArray indexOfObject:button];
    if (index == 0) {
        [self.tableView setContentOffset:CGPointMake(0, 0) animated:YES];
    }else{
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:index];
        [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
    }