iOS7 解决刷新重用cell时字体重复问题

不知道为啥刷新Cell的时候出现字体的重复,字体的覆盖现象。

google了下,发现解决的方法很简单的。如下代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CelebrityCheckTableCell";

//注册要使用的cell
[tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:CellIdentifier];

//创建cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:CellIdentifier];
}

//解决刷新重用cell事字体重复问题
NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
for(UIView *subview in subviews) {
[subview removeFromSuperview];
}

//获取行的个数
NSInteger row = [indexPath row];
NSDictionary *dict = [_dataDic objectAtIndex:row];
// NSLog(@"dict = %@",dict);

//cell高度参数
CGFloat cellHeight = cell.frame.size.height;
UIColor *rankBgColor = [[UIColor alloc] initWithRed:178.0/255.0 green:178.0/255.0 blue:178.0/255.0 alpha:1.0];
if(row <= 2){
rankBgColor = [[UIColor alloc] initWithRed:218.0/255.0 green:118.0/255.0 blue:159.0/255.0 alpha:1.0];
}

//排名 计算x,y
CGFloat rankWidth = 21.0;
CGFloat rankHeight = 21.0;
CGFloat rankX = 5.0;
CGFloat rankY = (cellHeight - rankHeight) / 2;
UILabel *rank = [[UILabel alloc] init];
[rank setFrame:CGRectMake(rankX, rankY, rankWidth, rankHeight)];
rank.backgroundColor = rankBgColor;
rank.textColor = [UIColor whiteColor];
rank.font = [UIFont boldSystemFontOfSize:17.0];
rank.textAlignment = NSTextAlignmentCenter;
[rank.layer setCornerRadius:2.0];
rank.text = [NSString stringWithFormat:@"%d",row+1];
[cell.contentView addSubview:rank];

//名称 计算x,y
CGFloat nameWidth = 150.0;
CGFloat nameHeight = 21.0;
CGFloat nameX = rankX + rankWidth + 5.0;
CGFloat nameY = (cellHeight - nameHeight) / 2;
UILabel *name = [[UILabel alloc] init];
[name setFrame:CGRectMake(nameX, nameY, nameWidth, nameHeight)];
name.textColor = [UIColor blackColor];
name.font = [UIFont boldSystemFontOfSize:17.0];
name.textAlignment = NSTextAlignmentLeft;
[name.layer setCornerRadius:2.0];
name.text = [NSString stringWithFormat:@"%@",[dict valueForKey:@"title"]];
[cell.contentView addSubview:name];

//分数 计算x,y
CGFloat scoreWidth = 100.0;
CGFloat scoreHeight = 21.0;
CGFloat scoreX = nameX + nameWidth + 5.0;
CGFloat scoreY = (cellHeight - scoreHeight) / 2;
UILabel *score = [[UILabel alloc] init];
[score setFrame:CGRectMake(scoreX, scoreY, scoreWidth, scoreHeight)];
score.textColor = [UIColor blackColor];
score.font = [UIFont boldSystemFontOfSize:16.0];
score.textAlignment = NSTextAlignmentRight;
[score.layer setCornerRadius:2.0];
score.text = [NSString stringWithFormat:@"%@",[dict valueForKey:@"check_sum"]];
[cell.contentView addSubview:score];

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}

得到的结果是,效果很好,解决了我的问题。

参考文章:

http://blog.csdn.net/developer_zhang/article/details/15026097

http://justcoding.iteye.com/blog/1476197

http://ddkangfu.blog.51cto.com/311989/465557/