大小分类tableView

// 左边一个大分类,右边一个小分类的tableview,滚动小分类大分类会自动滚动,选中大分类,对应的小分类会自动变色

#define BigFlag @"big"
#define SmallFlag @"small"
#define SmallTableViewHeaderHeight 1.0f
- (void)makeTableView
{
    CGFloat tableViewY = 30;
    CGFloat screenWidth = self.view.frame.size.width;
    CGFloat screenHeight = self.view.frame.size.height - tableViewY;
    CGFloat bigRatio = 0.3;
    
    self.bigTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, tableViewY, screenWidth*bigRatio, screenHeight) style:UITableViewStylePlain];
    self.bigTableView.delegate = self;
    self.bigTableView.dataSource = self;
    self.bigTableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.bigTableView];
    
    self.smallTableView = [[UITableView alloc] initWithFrame:CGRectMake(screenWidth*bigRatio, tableViewY, screenWidth*(1- bigRatio), screenHeight) style:UITableViewStylePlain];
    self.smallTableView.delegate = self;
    self.smallTableView.dataSource = self;
    self.smallTableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.smallTableView];
    
    
    self.datasource = [[NSMutableArray alloc] initWithCapacity:0];
    
    for (int i = 0; i< arc4random_uniform(20) + 20; i++) {
        
        NSMutableDictionary * bigdic = [NSMutableDictionary dictionaryWithCapacity:0];
        [bigdic setObject:[NSString stringWithFormat:@"我是第%d组", i] forKey:BigFlag];
        
        NSMutableArray *smallArr =[[NSMutableArray alloc] initWithCapacity:0];
        for (int j = 0; j<arc4random_uniform(20) + 10; j++) {
            [smallArr addObject:[NSString stringWithFormat:@"我是第%d组,第%d行",i, j]];
        }
        [bigdic setObject:smallArr forKey:SmallFlag];
        
        [self.datasource addObject:bigdic];
    }
    
    [self.bigTableView reloadData];
    [self.smallTableView reloadData];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == self.bigTableView) {
        return 1;
    }
    
    return self.datasource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.bigTableView) {
        return self.datasource.count;
    }
    
    NSMutableDictionary *dic = self.datasource[section];
    return [dic[SmallFlag] count];
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (self.bigTableView == tableView) {
        return nil;
    }
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.smallTableView.frame.size.width, SmallTableViewHeaderHeight)];
    
    view.backgroundColor = [UIColor orangeColor];
 
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (tableView == self.bigTableView) {
        return 0.001;
    }
    return SmallTableViewHeaderHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.bigTableView) {
    
        UITableViewCell *cellBig = [tableView dequeueReusableCellWithIdentifier:BigFlag];
        if (cellBig == nil) {
            cellBig = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:BigFlag];
            cellBig.textLabel.font = [UIFont systemFontOfSize:12];
        }
        
        cellBig.textLabel.textColor = [UIColor blackColor];
        
        if (self.indexPath == nil) {
            if (indexPath.row == 0) {
                cellBig.textLabel.textColor = [UIColor orangeColor];
            }
        } else {
            
            if (indexPath.row == self.indexPath.section) {
                cellBig.textLabel.textColor = [UIColor orangeColor];
            }
            
        }
        
        cellBig.textLabel.text = [[self.datasource objectAtIndex:indexPath.row] objectForKey:BigFlag];
        
        
        return cellBig;
    }
    
    UITableViewCell *cellSmall = [tableView dequeueReusableCellWithIdentifier:SmallFlag];
    if (cellSmall == nil) {
        cellSmall = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SmallFlag];
    }
    
    cellSmall.textLabel.textColor = [UIColor blackColor];
    
    if (self.indexPath == nil) {
        if (indexPath.section == 0) {
            
            cellSmall.textLabel.textColor = [UIColor orangeColor];
        }
    } else {
        
        if (self.indexPath.section == indexPath.section) {
            
            cellSmall.textLabel.textColor = [UIColor orangeColor];
        }
        
    }
    
   
    
    cellSmall.textLabel.text = [[[self.datasource objectAtIndex:indexPath.section] objectForKey:SmallFlag] objectAtIndex:indexPath.row];
    
    
    return cellSmall;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (self.smallTableView == tableView) {
        return;
    }
    
    self.indexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.row]; // 转换为SmallTableView的indexPath
    
    [self.smallTableView scrollToRowAtIndexPath:self.indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
    
    [self.smallTableView reloadData];
    [self.bigTableView reloadData];
}

#pragma mark - UIScrollViewDelegate Methods

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.bigTableView == scrollView) {
        
        
        return;
    }
    UITableViewCell *cell = self.smallTableView.visibleCells[0];
    NSIndexPath *indexPath = [self.smallTableView indexPathForCell:cell];
    
    self.indexPath = indexPath;
    
    // 判断拖拽过程中BigTableView的cell是否可见
    BOOL isTop = [self.bigTableView.visibleCells containsObject:[self.bigTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.indexPath.section inSection:0]]];
    
    if (!isTop) {
        [self.bigTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.indexPath.section inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
    }
//    [self.bigTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.indexPath.section inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
    
    [self.smallTableView reloadData];
    [self.bigTableView reloadData];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (self.bigTableView == scrollView) {
        
        return;
    }
    
    [self.bigTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.indexPath.section inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];

}

原文地址:https://www.cnblogs.com/jzlblog/p/4381062.html