关于iOS的tableView的cellForRowAtIndexPath
说明:
1. 如果是标准的cell,比如每个cell上的元素是一样的,那么可以直接利用缓存;
2.如果是非标准的cell,每个cell展示的元素会有差别,那么对这些有差别的视图组件要进行特殊处理。
2.1 特殊处理:
2.1.1 在缓存中获取到这些特殊的视图组件,必须将其从contentView中移除;
2.1.2 每次重新创建这些特殊视图组件(也就是说这些特殊元素是不能用缓存,否则会在界面上下滚动时候会发生错乱)。
3.添加到cell的contentView的子视图,必须为子视图定义唯一的tag属性,在获取缓存的时候可以通过tag属性进行相应的操作处理。
4. 在cell上进行操作后,例如改变了cell上显示的内容,必须去修改tableView对应的数据源,否则在滚动时发现已经修改掉的内容却还是显示出来。
#pragma mark - 绘制单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//-------定义界面展示需要的视图组件------------------
//时间和联赛
UILabel* timeAndClubName;
//主队和客队
UIImageView* ivTeam1Icon;
UIImageView* ivTeam2Icon;
UILabel* lblTeam1Name;
UILabel* lblTeam2Name;
//关注按钮
UIButton* btnAttention;
//比分
UILabel* lblScore;
//直播频道
UILabel* lblChannel;
//定义缓存标记
static NSString *identifier = @"cell_id";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
//-------创建界面展示需要的视图组件------------------
//时间和联赛
timeAndClubName = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, cell.width, 10)];
[timeAndClubName setFont:[UIFont systemFontOfSize:11]];
timeAndClubName.textAlignment = NSTextAlignmentCenter;
timeAndClubName.textColor = [UIColor lightGrayColor];
timeAndClubName.tag=TAG_START_INDEX+1;
//主队和客队
ivTeam1Icon = [[UIImageView alloc] initWithFrame:CGRectMake(40, 20, 40, 40)];
ivTeam1Icon.tag=TAG_START_INDEX+2;
ivTeam2Icon = [[UIImageView alloc] initWithFrame:CGRectMake(cell.width-80, 20, 40, 40)];
ivTeam2Icon.tag=TAG_START_INDEX+3;
lblTeam1Name = [[UILabel alloc]initWithFrame:CGRectMake(0, ivTeam1Icon.bottom +5, 120, 20)];
lblTeam1Name.tag=TAG_START_INDEX+4;
lblTeam1Name.textAlignment = NSTextAlignmentCenter;
[lblTeam1Name setFont:[UIFont systemFontOfSize:12]];
lblTeam2Name = [[UILabel alloc]initWithFrame:CGRectMake(cell.width-120, ivTeam2Icon.bottom+5 , 120, 20)];
lblTeam2Name.tag=TAG_START_INDEX+5;
lblTeam2Name.textAlignment = NSTextAlignmentCenter;
[lblTeam2Name setFont:[UIFont systemFontOfSize:12]];
//直播频道
lblChannel = [[UILabel alloc]initWithFrame:CGRectMake(0, timeAndClubName.bottom +50, 120, 20)];
lblChannel.center = CGPointMake(cell.center.x, lblChannel.frame.origin.y);
lblChannel.textAlignment = NSTextAlignmentCenter;
[lblChannel setFont:[UIFont systemFontOfSize:11]];
lblChannel.textColor = [UIColor lightGrayColor];
lblChannel.tag=TAG_START_INDEX+8;
}else{
//-------从缓存中获取界面展示需要的视图组件------------------
//时间和联赛
timeAndClubName = (UILabel*)[cell.contentView viewWithTag:TAG_START_INDEX+1];
//主队和客队
ivTeam1Icon = (UIImageView*)[cell.contentView viewWithTag:TAG_START_INDEX+2];
ivTeam2Icon = (UIImageView*)[cell.contentView viewWithTag:TAG_START_INDEX+3];
lblTeam1Name = (UILabel*)[cell.contentView viewWithTag:TAG_START_INDEX+4];
lblTeam2Name = (UILabel*)[cell.contentView viewWithTag:TAG_START_INDEX+5];
//关注按钮
UIView* view1 = [cell.contentView viewWithTag:TAG_START_INDEX+6];
if (view1) {
[view1 removeFromSuperview];
}
//比分
UIView* view2 = [cell.contentView viewWithTag:TAG_START_INDEX+7];
if (view2) {
[view2 removeFromSuperview];
}
//直播频道
lblChannel = (UILabel*)[cell.contentView viewWithTag:TAG_START_INDEX+8];
}
//----------关注按钮、比分 比较特殊(有些cell显示,有些不显示),因此每次需要重新创建 start----------------
//关注按钮
btnAttention = [UIButton buttonWithType:UIButtonTypeCustom];
btnAttention.frame = CGRectMake((cell.width-25)/2, timeAndClubName.bottom +11, 25, 25);
[btnAttention addTarget:self action:@selector(btnAttentionClick:) forControlEvents:UIControlEventTouchUpInside];
btnAttention.tag=TAG_START_INDEX+6;
//比分
lblScore = [[UILabel alloc]initWithFrame:CGRectMake(0, timeAndClubName.bottom +30, 120, 20)];
lblScore.center = CGPointMake(cell.center.x, lblScore.frame.origin.y);
lblScore.textAlignment = NSTextAlignmentCenter;
[lblScore setFont:[UIFont systemFontOfSize:18]];
lblScore.tag=TAG_START_INDEX+7;
//----------关注按钮、比分 比较特殊(有些cell显示,有些不显示),因此每次需要重新创建 end----------------
//设置cell被选中的效果
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//获取cell显示的数据源
NSDictionary *option = [_dicData objectForKey:[_categoryArray objectAtIndex:indexPath.section]][indexPath.row];
// UIView* mainView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, cell.width, cell.height)];
//时间和联赛
timeAndClubName.text=[NSString stringWithFormat:@"%@ %@",[[option objectForKey:@"time"] substringWithRange:NSMakeRange(11,5)],[option objectForKey:@"name"]];
//主队和客队
UIImage* placeHoldImage = [UIImage imageNamed:KGQ_DEFAULT_FOR_TEAM];
[MyHttpRequest downloadImage:[[option objectForKey:@"team1"] objectForKey:@"icon"] andWithImageShower:ivTeam1Icon andWithPlaceHoldIMage:placeHoldImage andBlock:^(UIImage *image) {}];
[MyHttpRequest downloadImage:[[option objectForKey:@"team2"] objectForKey:@"icon"] andWithImageShower:ivTeam2Icon andWithPlaceHoldIMage:placeHoldImage andBlock:^(UIImage *image) {}];
// [ivTeam2Icon sd_setImageWithURL:[[option objectForKey:@"team2"] objectForKey:@"icon"] placeholderImage:[UIImage imageNamed:KGQ_DEFAULT_FOR_TEAM]];
lblTeam1Name.text=[[option objectForKey:@"team1"] objectForKey:@"name"];
lblTeam2Name.text=[[option objectForKey:@"team2"] objectForKey:@"name"];
//关注按钮
BOOL isAttention = [[option objectForKey:@"attention_state"] isEqualToString:@"0"] ? NO : YES;
if (isAttention) {
[btnAttention setImage:[UIImage imageNamed:KGQ_IMAGE_ATTENTION_YES] forState:UIControlStateNormal];
}else{
[btnAttention setImage:[UIImage imageNamed:KGQ_IMAGE_ATTENTION_NO] forState:UIControlStateNormal];
}
//比分
lblScore.center = CGPointMake(cell.center.x, lblScore.frame.origin.y);
lblScore.text=[NSString stringWithFormat:@"%@:%@",[[option objectForKey:@"team1"] objectForKey:@"score"],[[option objectForKey:@"team2"] objectForKey:@"score"]];
/*
“state”:”比赛状态(0=未开始,1=进行中,2=已结束)”
*/
NSString* strState = [option objectForKey:@"state"];
NSInteger state = [strState intValue];
//直播频道
lblChannel.text=[option objectForKey:@"channel"];
if (state==2) {//比赛结束
lblChannel.text=@"已完场";
}
//加入contentView
if (state==0) {
[cell.contentView addSubview:btnAttention];
}else{
[cell.contentView addSubview:lblScore];
}
[cell.contentView addSubview:ivTeam1Icon];
[cell.contentView addSubview:ivTeam2Icon];
[cell.contentView addSubview:timeAndClubName];
[cell.contentView addSubview:lblTeam1Name];
[cell.contentView addSubview:lblTeam2Name];
[cell.contentView addSubview:lblChannel];
return cell;
}
本文介绍如何在iOS应用中高效地配置TableView的Cell,包括标准和非标准Cell的处理方法,特别关注视图组件的缓存及重用策略,确保界面响应速度与用户体验。
1万+

被折叠的 条评论
为什么被折叠?



