iOS中使用UISegmentControl进行UITableView切换

先上图:


我们经常在项目中使用到UISegmentControl进行UITableView的切换
下面我说说我实现的过程。

这个UISegmentControl我是放在导航栏的中间的,实现代码如下:

//创建导航栏分栏控件
-(void)createSegMentController{
    NSArray *segmentedArray = [NSArray arrayWithObjects:@"问答",@"倾述",nil];
    
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedArray];
    
    segmentedControl.frame = CGRectMake(0, 0, 180, 30);
    
    segmentedControl.selectedSegmentIndex = 0;
    segmentedControl.tintColor = [UIColor colorWithRed:252/255.0 green:245/255.0 blue:248/255.0 alpha:1];
    
    [segmentedControl addTarget:self action:@selector(indexDidChangeForSegmentedControl:) forControlEvents:UIControlEventValueChanged];
    
    [self.navigationItem setTitleView:segmentedControl];
}

下面讲讲如何实现UITableView的切换:

创建两个UITableView
UITableView *leftTable;
UITableView *rightTable;
    leftTable = [[UITableView alloc]initWithFrame:CGRectMake(0,64,WIDTH,HEIGHT-64-44) style:UITableViewStylePlain];
    leftTable.delegate = self;
    leftTable.dataSource = self;
    [leftTable registerNib:[UINib nibWithNibName:@"AnswerCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:AnswerReuseId];
    leftTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:leftTable];
    
    rightTable = [[UITableView alloc]initWithFrame:CGRectMake(0,64,WIDTH,HEIGHT-64-44) style:UITableViewStylePlain];
    rightTable.delegate = self;
    rightTable.dataSource = self;
    [rightTable registerNib:[UINib nibWithNibName:@"AnswerCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:AnswerReuseId];
    rightTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:rightTable];
  • 接下来我们实现切换:

-(void)indexDidChangeForSegmentedControl:(UISegmentedControl *)sender
{
//我定义了一个 NSInteger tag,是为了记录我当前选择的是分段控件的左边还是右边。
    NSInteger selecIndex = sender.selectedSegmentIndex;
    switch(selecIndex){
            case 0:
            leftTable.hidden = NO;
            rightTable.hidden = YES;
            sender.selectedSegmentIndex=0;
            tag = 0;
            [leftTable reloadData];
            break;
            
            case 1:
            leftTable.hidden = YES;
            rightTable.hidden = NO;
            sender.selectedSegmentIndex = 1;
            tag=1;
            [rightTable reloadData];
            break;
            
            default:
            break; 
    }
}

接下来实现UITableView的代理和数据源
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   
    if(tag==0){
        return array.count;
    }
    else if (tag==1){
        return array.count;
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(tag == 0){
        
        AnswerCell *cell = [leftTable dequeueReusableCellWithIdentifier:AnswerReuseId ];
        if (!cell) {
            cell = (AnswerCell *)[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier: AnswerReuseId];
        }
        //cell.model = array[indexPath.row];
        cell.content.text = array[indexPath.row];
        cell.userImg.image = [UIImage imageNamed:@"tabbar_home_selected@2x"];
        cell.userName.text = @"一个演员的自我修养";
        cell.title.text = @"演员的自我修养";
        cell.time.text = @"4小时前";
        cell.star.text  = @"1344";
        cell.counts.text = @"445";
        // 获取不确定文字的高度
        NSString *str = array[indexPath.row];
        CGFloat testHeight = [self sizeWithFont:[UIFont systemFontOfSize:14.0] maxW:cell.frame.size.width withContent:str] + 10;
        
        // 重新设置cell的frame.
        CGRect frame = cell.frame;
        frame.size.height = testHeight + self.originCellMaxY;
        cell.frame = frame;
        return cell;
    }
    else if (tag==1){
        AnswerCell *cell = [rightTable dequeueReusableCellWithIdentifier:AnswerReuseId ];
        if (!cell) {
            cell = (AnswerCell *)[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier: AnswerReuseId];
        }
        //cell.model = array[indexPath.row];
        cell.content.text = array[indexPath.row];
        cell.userImg.image = [UIImage imageNamed:@"tabbar_home_selected@2x"];
        cell.userName.text = @"一个演员的自我修养";
        cell.title.text = @"演员的自我修养";
        cell.time.text = @"4小时前";
        cell.star.text  = @"1344";
        cell.counts.text = @"445";
        // 获取不确定文字的高度
        NSString *str = array[indexPath.row];
        CGFloat testHeight = [self sizeWithFont:[UIFont systemFontOfSize:14.0] maxW:cell.frame.size.width withContent:str] + 10;
        
        // 重新设置cell的frame.
        CGRect frame = cell.frame;
        frame.size.height = testHeight + self.originCellMaxY;
        cell.frame = frame;
        return cell;
    }
    
    return nil;
    
}


效果图是这样的:

iOS中使用UISegmentControl进行UITableView切换_第1张图片
iOS中使用UISegmentControl进行UITableView切换_第2张图片
对了说一下,每个单元格之间那段距离 我是在自定义的cell中放了一个高度为10的UIView。哈哈

就是这个样子:

iOS中使用UISegmentControl进行UITableView切换_第3张图片

你可能感兴趣的:(iOS中使用UISegmentControl进行UITableView切换)