我必须填充行数。当我使用
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
这一行,在滚动到某些行后,它会在同一位置重绘行。请在屏幕下方查看
如果我使单元格为零,即
UITableViewCell *cell = nil;
然后工作正常,但它需要更多的记忆。现在我的问题是它为什么会发生。
将单元格nil重绘在相同位置的关系是什么?
这是我的代码:
- (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];
}
UIButton *questionButton=[UIButton buttonWithType:UIButtonTypeCustom];
questionButton.backgroundColor=[UIColor clearColor];
questionButton.tag=indexPath.row+1;
[questionButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
UIImageView *statusImageView=[[UIImageView alloc]initWithFrame:CGRectMake(190,25,23,23)];
NSString *statusImageName=[[NSString alloc]init];
statusImageName=@"Right";
[statusImageView setImage:[UIImage imageNamed:statusImageName]];
[questionButton addSubview:statusImageView];
if(indexPath.row%2==0)
{
[questionButton setBackgroundImage:[UIImage imageNamed:@"TopicsbarLeft"] forState:UIControlStateNormal];
[questionButton setBackgroundImage:[UIImage imageNamed:@"TopicsbarLetSelected"] forState:UIControlStateHighlighted];
CGRect topicButtonFrame=CGRectMake(0, 10, 250, 70);
questionButton.frame=topicButtonFrame;
NSLog(@"Even Rows =%d",indexPath.row);
NSString *questionLabel=[NSString stringWithFormat:@" Question %d :",indexPath.row+1];
[questionButton setTitle:questionLabel forState:UIControlStateNormal];
//questionButton.titleLabel.textAlignment=NSTextAlignmentLeft;
[questionButton.titleLabel setTextAlignment:UITextAlignmentLeft];
}
else
{
[questionButton setBackgroundImage:[UIImage imageNamed:@"TopicsbarRight"] forState:UIControlStateNormal];
[questionButton setBackgroundImage:[UIImage imageNamed:@"TopicsbarRightSelected"] forState:UIControlStateHighlighted];
CGRect topicButtonFrame=CGRectMake(100, 10, 250, 70);
questionButton.frame=topicButtonFrame;
NSString *questionLabel=[NSString stringWithFormat:@" Question %d :",indexPath.row+1];
[questionButton setTitle:questionLabel forState:UIControlStateNormal];
questionButton.titleLabel.textAlignment=UITextAlignmentRight;
[cell addSubview:questionButton];
}
[cell addSubview:questionButton];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
return cell;
}
答案 0 :(得分:2)
dequeueReusableCellWithIdentifier
的目的是减少使用内存。如果屏幕可以容纳4或5个表格单元格,那么重复使用时,即使表格有1000个条目,也只需要在内存中分配4或5个表格单元格。
因此,当您执行此操作UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
时,它会返回旧单元格以供重用。它不是一个新的单元格实例。它包含初始化为旧数据的单元格的UI元素。要再次显示它,您需要重置所有UI元素并输入新值。
放置UITableViewCell *cell = nil;
时的第二种方法是不重用。第二种方式没有优势,只使用一组表格单元格。如果您的表有1000个条目,那么您将在内存中分配1000个单元格。如果你打算这样做,你会把它们放在一个数组中,然后用行号索引数组并返回单元格。这应该可以解释你的内存使用量的高峰,因为它没有重用任何单元实例。
对于具有固定单元的小型表,第二种解决方案可能是一种合理的解决方案,对于动态或大型表,重用是最好的方法...
更新:,因为您要求提供一些代码。另请注意,根据奇数或偶数,您正在进行两种类型的渲染。因此,当您想显示偶数编号的问题时,来自reuse
的单元格可能会很奇怪。在这种情况下,您需要在获取单元格之后重置单元格(从重用或创建新单元格)并让您的正常逻辑流过......
[questionButton setBackgroundImage:[UIImage imageNamed:@"default.png"]];
[questionButton setTitle:@"" forState:UIControlStateNormal];
CGRect topicButtonFrame=CGRectMake(0, 0, 250, 70);
questionButton.frame=topicButtonFrame;
答案 1 :(得分:0)
问题在于:
[cell addSubview:questionButton];
}
[cell addSubview:questionButton];
您要添加两次问题按钮。请删除其他部分,然后尝试。
更新:
你可以试试这个只是为了确认问题是什么:
if(indexPath.row%2==0)
{
UIButton *questionButton=[UIButton buttonWithType:UIButtonTypeCustom];
questionButton.backgroundColor=[UIColor clearColor];
questionButton.tag=indexPath.row+1;
[questionButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
UIImageView *statusImageView=[[UIImageView alloc]initWithFrame:CGRectMake(190,25,23,23)];
NSString *statusImageName=[[NSString alloc]init];
statusImageName=@"Right";
[statusImageView setImage:[UIImage imageNamed:statusImageName]];
[questionButton addSubview:statusImageView];
[questionButton setBackgroundImage:[UIImage imageNamed:@"TopicsbarLeft"] forState:UIControlStateNormal];
[questionButton setBackgroundImage:[UIImage imageNamed:@"TopicsbarLetSelected"] forState:UIControlStateHighlighted];
CGRect topicButtonFrame=CGRectMake(0, 10, 250, 70);
questionButton.frame=topicButtonFrame;
NSString *questionLabel=[NSString stringWithFormat:@" Question %d :",indexPath.row+1];
[questionButton setTitle:questionLabel forState:UIControlStateNormal];
//questionButton.titleLabel.textAlignment=NSTextAlignmentLeft;
[questionButton.titleLabel setTextAlignment:UITextAlignmentLeft];
[cell addSubview:questionButton];
}
else
{
UIButton *questionButton=[UIButton buttonWithType:UIButtonTypeCustom];
questionButton.backgroundColor=[UIColor clearColor];
questionButton.tag=indexPath.row+1;
[questionButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
UIImageView *statusImageView=[[UIImageView alloc]initWithFrame:CGRectMake(190,25,23,23)];
NSString *statusImageName=[[NSString alloc]init];
statusImageName=@"Right";
[statusImageView setImage:[UIImage imageNamed:statusImageName]];
[questionButton addSubview:statusImageView];
[questionButton setBackgroundImage:[UIImage imageNamed:@"TopicsbarRight"] forState:UIControlStateNormal];
[questionButton setBackgroundImage:[UIImage imageNamed:@"TopicsbarRightSelected"] forState:UIControlStateHighlighted];
CGRect topicButtonFrame=CGRectMake(100, 10, 250, 70);
questionButton.frame=topicButtonFrame;
NSString *questionLabel=[NSString stringWithFormat:@" Question %d :",indexPath.row+1];
[questionButton setTitle:questionLabel forState:UIControlStateNormal];
questionButton.titleLabel.textAlignment=UITextAlignmentRight;
[cell addSubview:questionButton];
}