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]; //加入contentView [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; }
问题:如果先定义一个UIView *mainView,然后把上面代码中的标签、按钮、图片视图先全部加到mainView中,然后再把mainView加到cell.contentView上,会发生一个奇怪的问题:运行后,按钮的点击事件不起作用,但是把表格向下或向上滚动拉出更多的cell后,按钮的点击事件又可以使用了!