具有重绘问题的可滚动表。似乎没有清理 [英] Scrollable table having a redraw issue. Doesnt seem to be clearing

查看:109
本文介绍了具有重绘问题的可滚动表。似乎没有清理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可滚动的表格。
在每个单元格中,我绘制了3个UILabel。前几个单元格正常。但是当我向下滚动桌子时,UILabels似乎正在吸引以前的UILabels。它就像单元格中的标签有一个已经清除的旧标签。我可以通过在每个重绘的整个单元格上绘制背景颜色来解决这个问题,但这仍然无法解释这个奇怪的问题及其发生的原因。

I have a scrollable table. In each cell I draw 3 UILabels. The first few cells draw ok. But as I scroll down the table the UILabels seems to be drawing over previous UILabels. Its like the Labels in the cell have an old label already there that was cleared. I could probably fix this by drawing a background color over the whole cell each redraw, but that still doesn't explain this strange problem and why its happening.

任何人都知道为什么这会发生什么解决方案?

Anybody know why this occurs and what a solution might be?

- (UITableViewCell *)tableView:(UITableView *)tableView 
cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
static NSString *CellIdentifier = @"Cell";    
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
reuseIdentifier:CellIdentifier] autorelease];
}

Match *aMatch = [appDelegate.matchScoresArray objectAtIndex:indexPath.row];

UILabel * teamName1Label = [[UILabel alloc]initWithFrame:CGRectMake(5,5, 100, 20)];
teamName1Label.textAlignment = UITextAlignmentCenter;
teamName1Label.textColor =  [UIColor redColor];
teamName1Label.backgroundColor = [UIColor clearColor];
teamName1Label.text = aMatch.teamName1;
[cell addSubview:teamName1Label];

UILabel *teamVersusLabel = [[UILabel alloc]initWithFrame:CGRectMake(115,5, 40, 20)];
teamVersusLabel.textAlignment = UITextAlignmentCenter;
teamVersusLabel.textColor =  [UIColor redColor];
teamVersusLabel.backgroundColor = [UIColor clearColor];
teamVersusLabel.text = @"V";
[cell addSubview:teamVersusLabel];


UILabel *teamName2Label = [[UILabel alloc]initWithFrame:CGRectMake(155,5, 100, 20)];
teamName2Label.textAlignment = UITextAlignmentCenter;
teamName2Label.textColor =  [UIColor redColor];
teamName2Label.backgroundColor = [UIColor clearColor];
teamName2Label.text = aMatch.teamName2;
[cell addSubview:teamName2Label];

return cell;
}

非常感谢
-Code

Many Thanks -Code

推荐答案

我认为这个问题的最佳解决方案是在
中定义这些标签 - (UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *) identifier withIndexPath:(NSIndexPath *)indexPath 方法

I think the best solution for this problem is to define these labels in the -(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath method

-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]autorelease];

UILabel * teamName1Label = [[UILabel alloc]initWithFrame:CGRectMake(5,5, 100, 20)];
teamName1Label.textAlignment = UITextAlignmentCenter;
teamName1Label.textColor =  [UIColor redColor];
teamName1Label.backgroundColor = [UIColor clearColor];
teamName1Label.text = aMatch.teamName1;
teamName1Label.tag = 1;
[cell.contentView addSubview:teamName1Label];
[teamName1Label release];

UILabel *teamVersusLabel = [[UILabel alloc]initWithFrame:CGRectMake(115,5, 40, 20)];
teamVersusLabel.textAlignment = UITextAlignmentCenter;
teamVersusLabel.textColor =  [UIColor redColor];
teamVersusLabel.backgroundColor = [UIColor clearColor];
teamVersusLabel.text = @"V";
teamVersusLabel.tag = 2;
[cell.contentView addSubview:teamVersusLabel];
[teamVersusLabel release];


UILabel *teamName2Label = [[UILabel alloc]initWithFrame:CGRectMake(155,5, 100, 20)];
teamName2Label.textAlignment = UITextAlignmentCenter;
teamName2Label.textColor =  [UIColor redColor];
teamName2Label.backgroundColor = [UIColor clearColor];
teamName2Label.text = aMatch.teamName2;
teamName2Label.tag = 3;
[cell.contentView addSubview:teamName2Label];
[teamName2Label release];

return cell;
}

现在cellForRowAtIndexPath方法将是 -

Now the cellForRowAtIndexPath method will be--

- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell=nil; 

    static NSString *Identifier = @"Cell";
    cell = [theTableView dequeueReusableCellWithIdentifier:Identifier];
    if(cell == nil){
    cell = [self reuseTableViewCellWithIdentifier:Identifier withIndexPath:indexPath];
    }

    Match *aMatch = [appDelegate.matchScoresArray objectAtIndex:indexPath.row];

    UILabel *label = (UILabel *)[cell.contentView viewWithTag:1];
    label.text = aMatch.teamName1;

    label = (UILabel *)[cell.contentView viewWithTag:2];
    label.text = @"V";

    label = (UILabel *)[cell.contentView viewWithTag:3];
    label.text = aMatch.teamName2;

    return cell;
    }

试试这个代码..希望它有帮助:)

Just try this code..Hope it helps :)

这篇关于具有重绘问题的可滚动表。似乎没有清理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆