分段表视图cell乱的问题(分段表示图cellForRowAtIndexPath的正确实现)

好久就想把这篇博客给整理出来了,前期项目很忙,今天有空写写。

        问题是:实现类似于微信通讯录的功能,可能一看,这个不是很简答吗,是的,比较简单,但是,做了才知道会碰到问题的,毕竟实践与想象的不一样。

       首先,这不是一个普通的表示图,这是一个分段的表示图,主要由三段:1.搜索框;2.新的朋友,校园中心,添加好友;3.才是通讯录中的联系人。前两段都是固定的,第三段是活动的,数量不固定的。

那么,在cellForRowAtIndexPath中的问题来了,因为这是三段不同的cell,也就是三种不同的cell,如果当成同一种cell来做的话,将会出现滑动几次后,全乱了。

解决这个问题有两种办法:

1、构造一个能适应这三种cell的综合性的cell(具体是把三种不同的cell的需要addsubview上的view都add到cell上去),然后在需要展现的行hidden属性设置为no,在不需要展示的行hidden属性设置为yes,本文就是这样实现的。具体看下面实现的代码。

2.构造三种不同的cell,用三种不同的cell来进行复用。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    

    static NSString *inde = @"cell";

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:inde];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:inde];

        

        UISearchBar* searchbar = [[UISearchBar alloc]initWithFrame:CGRectMake(5, 0, 310, 50)];

        searchbar.tag = 1000;

        searchbar.delegate = self;

        [cell addSubview:searchbar];

        for (UIView *view in searchbar.subviews) {

            // for before iOS7.0

            if ([view isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {

                [view removeFromSuperview];

                break;

            }

            // for later iOS7.0(include)

            if ([view isKindOfClass:NSClassFromString(@"UIView")] && view.subviews.count > 0) {

                [[view.subviews objectAtIndex:0] removeFromSuperview];

                break;

            }

        }

        

        

        UIImageView* imgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 40, 40)];

        imgView.tag = 1001;

        [cell addSubview:imgView];

        

        UILabel* lbl = [[UILabel alloc]initWithFrame:CGRectMake(55, 5, 100, 40)];

        lbl.tag = 1002;

        [cell addSubview:lbl];

        

        

        UIImageView* imgView1 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 40, 40)];

        imgView1.tag = 1003;

        [cell addSubview:imgView1];

        

        UILabel* lbl1 = [[UILabel alloc]initWithFrame:CGRectMake(55, 5, 100, 40)];

        lbl1.tag = 1004;

        [cell addSubview:lbl1];

        

        UIImageView* imgView2 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 40, 40)];

        imgView2.tag = 1005;

        [cell addSubview:imgView2];

        

        UILabel* lbl2 = [[UILabel alloc]initWithFrame:CGRectMake(55, 5, 100, 40)];

        lbl2.tag = 1006;

        [cell addSubview:lbl2];

        

        

        

        UILabel* lbl3 = [[UILabel alloc]initWithFrame:CGRectMake(55, 5, 100, 40)];

        lbl3.tag = 1007;

        [cell addSubview:lbl3];

        

        

        

        

        

        

    }

    {

        if (indexPath.section == 0) {

            UISearchBar* searchbar =(UISearchBar*)[cell viewWithTag:1000];

            searchbar.placeholder = @"请输入账号/昵称";

            searchbar.hidden = NO;

            cell.backgroundColor = SELF_VIEW_BACKGROUND_COLOR;

            

            UIImageView* imgView = (UIImageView*)[cell viewWithTag:1001];

            UILabel* lbl = (UILabel*)[cell viewWithTag:1002];

            imgView.hidden = YES;

            lbl.hidden = YES;

            

            

            UIImageView* imgView1 = (UIImageView*)[cell viewWithTag:1003];

            UILabel* lbl1 = (UILabel*)[cell viewWithTag:1004];

            imgView1.hidden = YES;

            lbl1.hidden = YES;

            

            

            UIImageView* imgView2 = (UIImageView*)[cell viewWithTag:1005];

            UILabel* lbl2= (UILabel*)[cell viewWithTag:1006];

            imgView2.hidden = YES;

            lbl2.hidden = YES;

            

            UILabel* lbl3 = (UILabel*)[cell viewWithTag:1007];

            lbl3.hidden = YES;

            

        }

        else if (indexPath.section == 1)

        {

            cell.backgroundColor = [UIColor whiteColor];

            

            if (indexPath.row == 0) {

                

                UIImageView* imgView = (UIImageView*)[cell viewWithTag:1001];

                imgView.image = [UIImage imageNamed:@"taddfriends"];

                UILabel* lbl = (UILabel*)[cell viewWithTag:1002];

                lbl.text= @"新的朋友";

                imgView.hidden = NO;

                lbl.hidden = NO;

                

                

                UISearchBar* searchbar =(UISearchBar*)[cell viewWithTag:1000];

                searchbar.hidden = YES;

                

                

                

                

                UIImageView* imgView1 = (UIImageView*)[cell viewWithTag:1003];

                UILabel* lbl1 = (UILabel*)[cell viewWithTag:1004];

                imgView1.hidden = YES;

                lbl1.hidden = YES;

                

                

                UIImageView* imgView2 = (UIImageView*)[cell viewWithTag:1005];

                UILabel* lbl2= (UILabel*)[cell viewWithTag:1006];

                imgView2.hidden = YES;

                lbl2.hidden = YES;

                

                UILabel* lbl3 = (UILabel*)[cell viewWithTag:1007];

                lbl3.hidden = YES;

                

                

                

            } else if (indexPath.row == 1){

                

                UIImageView* imgView = (UIImageView*)[cell viewWithTag:1003];

                imgView.image = [UIImage imageNamed:@"tschool"];

                UILabel* lbl = (UILabel*)[cell viewWithTag:1004];

                lbl.text= @"校园中心";

                imgView.hidden = NO;

                lbl.hidden = NO;

                

                UISearchBar* searchbar =(UISearchBar*)[cell viewWithTag:1000];

                searchbar.hidden = YES;

                

                

                

                

                UIImageView* imgView1 = (UIImageView*)[cell viewWithTag:1001];

                UILabel* lbl1 = (UILabel*)[cell viewWithTag:1002];

                imgView1.hidden = YES;

                lbl1.hidden = YES;

                

                

                UIImageView* imgView2 = (UIImageView*)[cell viewWithTag:1005];

                UILabel* lbl2= (UILabel*)[cell viewWithTag:1006];

                imgView2.hidden = YES;

                lbl2.hidden = YES;

                

                UILabel* lbl3 = (UILabel*)[cell viewWithTag:1007];

                lbl3.hidden = YES;

                

                

            }else if (indexPath.row == 2){

                UIImageView* imgView = (UIImageView*)[cell viewWithTag:1005];

                imgView.image = [UIImage imageNamed:@"tnewfriend"];

                UILabel* lbl = (UILabel*)[cell viewWithTag:1006];

                lbl.text= @"添加好友";

                

                imgView.hidden = NO;

                lbl.hidden = NO;

                

                

                UISearchBar* searchbar =(UISearchBar*)[cell viewWithTag:1000];

                searchbar.hidden = YES;

                

                

                

                

                UIImageView* imgView1 = (UIImageView*)[cell viewWithTag:1003];

                UILabel* lbl1 = (UILabel*)[cell viewWithTag:1004];

                imgView1.hidden = YES;

                lbl1.hidden = YES;

                

                

                UIImageView* imgView2 = (UIImageView*)[cell viewWithTag:1001];

                UILabel* lbl2= (UILabel*)[cell viewWithTag:1002];

                imgView2.hidden = YES;

                lbl2.hidden = YES;

                

                UILabel* lbl3 = (UILabel*)[cell viewWithTag:1007];

                lbl3.hidden = YES;

            }

            

        }

        else if (indexPath.section == 2) {

            

            UIImageView* imgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 40, 40)];

            NSData* data = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:[arr_headimage objectAtIndex:indexPath.row]]];

            imgView.image = [[UIImage alloc]initWithData:data];

            [cell addSubview:imgView];

            cell.backgroundColor = [UIColor whiteColor];

            

            

            UILabel* lbl = (UILabel*)[cell viewWithTag:1007];

            lbl.text= [arr_nickname objectAtIndex:indexPath.row];

            lbl.hidden = NO;

            

            UISearchBar* searchbar =(UISearchBar*)[cell viewWithTag:1000];

            searchbar.hidden = YES;

            

            

            

            

            UIImageView* imgView1 = (UIImageView*)[cell viewWithTag:1001];

            UILabel* lbl1 = (UILabel*)[cell viewWithTag:1002];

            imgView1.hidden = YES;

            lbl1.hidden = YES;

            

            

            UIImageView* imgView2 = (UIImageView*)[cell viewWithTag:1003];

            UILabel* lbl2= (UILabel*)[cell viewWithTag:1004];

            imgView2.hidden = YES;

            lbl2.hidden = YES;

            

            

            UIImageView* imgView3 = (UIImageView*)[cell viewWithTag:1005];

            UILabel* lbl3= (UILabel*)[cell viewWithTag:1006];

            imgView3.hidden = YES;

            lbl3.hidden = YES;

            

            

            

        }

    }

    

    return  cell;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值