我在uitableviewcell内的标签文字上使用了NSTimer。这个标签文本每5秒更新一次,我每5秒钟调用一次[重新加载theTableView];而不是更新它似乎重复而不是覆盖如何来:
以下是重新加载数据时的图像:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
chatCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) cell = [[chatCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.backgroundColor = [UIColor colorWithRed:243/255.0f green:243/255.0f blue:247/255.0f alpha:1.0f];
cell.homeTeamLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 10, 140, 20)];
cell.homeTeamLabel.text = [[arrayBarclay objectAtIndex:indexPath.row] objectForKey:@"hometeam" ];
cell.homeTeamLabel.font = [UIFont systemFontOfSize:14.0];
cell.homeTeamLabel.textColor = [UIColor blackColor];
[cell.contentView addSubview:cell.homeTeamLabel];
cell.awayTeamLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 30, 140, 20)];
cell.awayTeamLabel.text = [[arrayBarclay objectAtIndex:indexPath.row] objectForKey:@"awayteam" ];
cell.awayTeamLabel.font = [UIFont systemFontOfSize:14.0];
cell.awayTeamLabel.textColor = [UIColor blackColor];
[cell.contentView addSubview:cell.awayTeamLabel];
cell.homeTeamScore = [[UILabel alloc] initWithFrame:CGRectMake(cell.frame.size.width-50, 10, 30, 20)];
cell.homeTeamScore.text = [[arrayBarclay objectAtIndex:indexPath.row] objectForKey:@"homescore" ];
cell.homeTeamScore.font = [UIFont systemFontOfSize:14.0];
cell.homeTeamScore.textColor = [UIColor blackColor];
[cell.contentView addSubview:cell.homeTeamScore];
cell.awayTeamScore = [[UILabel alloc] initWithFrame:CGRectMake(cell.frame.size.width-50, 30, 30, 20)];
cell.awayTeamScore.text = [[arrayBarclay objectAtIndex:indexPath.row] objectForKey:@"awayscore" ];
cell.awayTeamScore.font = [UIFont systemFontOfSize:14.0];
cell.awayTeamScore.textColor = [UIColor blackColor];
[cell.contentView addSubview:cell.awayTeamScore];
cell.time = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 40, 60)];
cell.time.textAlignment = NSTextAlignmentCenter;
cell.time.text = [[arrayBarclay objectAtIndex:indexPath.row] objectForKey:@"time" ];
cell.time.font = [UIFont systemFontOfSize:14.0];
cell.time.textColor = [UIColor blackColor];
[cell.contentView addSubview:cell.time];
}
答案 0 :(得分:1)
不要在cellForRowAtIndexPath中使用[cell addSubview:yourView];
,它会继续在您的单元格上添加子视图,无论它是否存在。子类UITableViewCell并分配所有视图并在初始化中添加它们,然后引用这些属性并在NSTimer调用的方法中更新它们的方式。
答案 1 :(得分:1)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
chatCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil){
cell = [[chatCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
//It's better to move following code to chatCell
//And ChatCell className will also be better.
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.backgroundColor = [UIColor colorWithRed:243/255.0f green:243/255.0f blue:247/255.0f alpha:1.0f];
cell.homeTeamLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 10, 140, 20)];
cell.homeTeamLabel.font = [UIFont systemFontOfSize:14.0];
cell.homeTeamLabel.textColor = [UIColor blackColor];
[cell.contentView addSubview:cell.homeTeamLabel];
cell.awayTeamLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 30, 140, 20)];
cell.awayTeamLabel.font = [UIFont systemFontOfSize:14.0];
cell.awayTeamLabel.textColor = [UIColor blackColor];
[cell.contentView addSubview:cell.awayTeamLabel];
cell.homeTeamScore = [[UILabel alloc] initWithFrame:CGRectMake(cell.frame.size.width-50, 10, 30, 20)];
cell.homeTeamScore.font = [UIFont systemFontOfSize:14.0];
cell.homeTeamScore.textColor = [UIColor blackColor]
[cell.contentView addSubview:cell.homeTeamScore];
cell.awayTeamScore = [[UILabel alloc] initWithFrame:CGRectMake(cell.frame.size.width-50, 30, 30, 20)];
cell.awayTeamScore.font = [UIFont systemFontOfSize:14.0];
cell.awayTeamScore.textColor = [UIColor blackColor];
[cell.contentView addSubview:cell.awayTeamScore];
cell.time = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 40, 60)];
cell.time.textAlignment = NSTextAlignmentCenter;
cell.time.font = [UIFont systemFontOfSize:14.0];
cell.time.textColor = [UIColor blackColor];
[cell.contentView addSubview:cell.time];
}
//Move [arrayBarclay objectAtIndex:indexPath.row] out will be better.
NSDictionnary *contentDict = [arrayBarclay objectAtIndex:indexPath.row];
cell.homeTeamLabel.text = [contentDict objectForKey:@"hometeam"];
cell.awayTeamLabel.text = [contentDict objectForKey:@"awayteam"];
cell.homeTeamScore.text = [contentDict objectForKey:@"homescore" ];
cell.awayTeamScore.text = [contentDict objectForKey:@"awayscore" ];
cell.time.text = [contentDict objectForKey:@"time" ];
return cell;
}
如果要重用单元格,最好在其init方法中初始化其子视图。只要显示单元格,就会调用cellForRowAtIndexPath:
。如果您需要动态addsubview
,请先删除以前添加的视图。