当前位置: 首页 > 图灵资讯 > 技术篇> iOS表格绘制cell-往cell.contentView添加子视图

iOS表格绘制cell-往cell.contentView添加子视图

来源:图灵教育
时间:2023-06-14 09:48:39

IOS表格绘制cell-往cell.contentview添加子视图

当您动态修改cell中的视图时,如UILabel上的文本、UIImageview的图片等UIButton,必须先删除cell.contentview中自定义的视图,代码:

//删除cell.contentview中新添加的视图        for(UIView *view in [cell.contentView subviews])        {            [view removeFromSuperview];        }

注:如果不添加上述代码,则表格上下滚动时,contentview上的标签、按钮和图片会因缓存而重叠。

#pragma mark - 绘制单元格- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    NSLog(@"-------cellForRowAtIndexPath");    static NSString *identifier = @"cell_id";    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];    if (!cell) {        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];    }else{        //删除cell.contentview中新添加的视图        for(UIView *view in [cell.contentView subviews])        {            [view removeFromSuperview];        }    }    cell.selectionStyle = UITableViewCellSelectionStyleNone;    NSDictionary *option = [_dicData objectForKey:[_categoryArray objectAtIndex:indexPath.section]][indexPath.row];//    UIView* mainView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, cell.width, cell.height)];    /时间和联赛    UILabel* timeAndClubName = [[UILabel alloc]initWithFrame:CGRectMake(0, 12, cell.width, 10)];    timeAndClubName.text=[NSString stringWithFormat:@"%@ %@",[[option objectForKey:@"time"] substringWithRange:NSMakeRange(11,5)],[option objectForKey:@"name"]];    [timeAndClubName setFont:[UIFont systemFontOfSize:13]];    timeAndClubName.textAlignment = NSTextAlignmentCenter;    timeAndClubName.textColor = [UIColor lightGrayColor];    //主队和客队    UIImageView* ivteam1Icon = [[UIImageView alloc] initWithFrame:CGRectMake(40, 20, 40, 40)];    [ivteam1Icon sd_setImageWithURL:[[option objectForKey:@"team1" objectForKey:@"icon"] placeholderImage:[UIImage imageNamed:KGQ_DEFAULT_FOR_TEAM]];        UIImageView* ivteam2Icon = [[UIImageView alloc] initWithFrame:CGRectMake(cell.width-80, 20, 40, 40)];    [ivteam2Icon sd_setImageWithURL:[[option objectForKey:@"team2" objectForKey:@"icon"] placeholderImage:[UIImage imageNamed:KGQ_DEFAULT_FOR_TEAM]];        UILabel* lblteam1Name = [[UILabel alloc]initWithFrame:CGRectMake(0, ivteam1Icon.bottom +5, 120, 20)];    lblteam1Name.text=[[option objectForKey:@"team1" objectForKey:@"name"];    lblteam1Name.textAlignment = NSTextAlignmentCenter;    UILabel* lblteam2Name = [[UILabel alloc]initWithFrame:CGRectMake(cell.width-120, ivteam2Icon.bottom+5 , 120, 20)];    lblteam2Name.text=[[option objectForKey:@"team2" objectForKey:@"name"];    lblteam2Name.textAlignment = NSTextAlignmentCenter;    //注意按钮或比分    BOOL isAttention = [[option objectForKey:@"attention_state"] isEqualToString:@"0"] ? NO : YES;    UIButton* btnAttention = [UIButton buttonWithType:UIButtonTypeCustom];    btnAttention.frame = CGRectMake((cell.width-25)/2, timeAndClubName.bottom +15, 25, 25);    if (isAttention) {        [btnAttention setImage:[UIImage imageNamed:@"img_collect"] forState:UIControlStateNormal];    }else{        [btnAttention setImage:[UIImage imageNamed:@"img_uncollect"] forState:UIControlStateNormal];    }    [btnAttention addTarget:self action:@selector(btnAttentionClick:) forControlEvents:UIControlEventTouchUpInside];        //加入contentviewiew    [cell.contentView addSubview:btnAttention];    [cell.contentView addSubview:ivteam1Icon];    [cell.contentView addSubview:ivteam2Icon];    [cell.contentView addSubview:timeAndClubName];    [cell.contentView addSubview:lblteam1Name];    [cell.contentView addSubview:lblteam2Name];    return cell;}

问题:如果先定义一个UIViewew *mainView,然后在mainview中添加上述代码中的所有标签、按钮和图片视图,然后在cell中添加mainview.在contentview上,会出现一个奇怪的问题:运行后,按钮的点击事件不起作用,但将表格向下或向上滚动,拉出更多的cell,再次使用按钮点击事件!