我必须填充行数。当我使用
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
这一行,在滚动到某些行后,它会在同一位置重绘行。见下面的屏幕
如果我让细胞为零,即
UITableViewCell *cell = 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;
}