2010-04-26 60 views
1

我在使用dequeueReusableCellWithIdentifier方法时遇到问题。每当我使用这个单元格时,一个单元格会显示自己的值和不属于它的值。这firstsecond图像显示的问题,而third图像显示正确的数据。这个问题似乎是由旧价值没有被删除和新的价值被添加到顶部,如果它。这似乎很奇怪。UITableViewCell使用来自其他单元格的值

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell =nil ; 
if(indexPath.row==0){ 
    cell = [[[UITableViewCell alloc]init]autorelease]; 
}else { 
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
} 

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


} 

NSMutableArray *rowArray = [results objectAtIndex:indexPath.section]; 
NSString *value=nil; 

if (indexPath.section==0) { 
    // the row in the 0th section is patient's sticky 
    value = [rowArray objectAtIndex:indexPath.row]; 
    cell.textLabel.text = value; 
}else { 
    //the row in the 1th section are lab tests 
    //here rowArray is tableRecords 
    NSArray *rowrecordTemp = [rowArray objectAtIndex:indexPath.row]; 
    UILabel *label= [[UILabel alloc]initWithFrame:CGRectMake(5, 0, 130, tableView.rowHeight-5) ];  
    value = [rowrecordTemp objectAtIndex:0]; 

    label.text= value; 
    [cell.contentView addSubview:label]; 
    [label release]; 
    label = nil; 


    label = [[UILabel alloc] initWithFrame:CGRectMake(140, 0, 50.0, 
                 tableView.rowHeight-10)]; 
    value = [rowrecordTemp objectAtIndex:1]; 
    label.text = value; 
    [cell.contentView addSubview:label]; 
    [label release]; 
    label = nil; 


    label = [[UILabel alloc] initWithFrame:CGRectMake(190, 0, 90, tableView.rowHeight-10)]; 
    value = [rowrecordTemp objectAtIndex:2]; 
    label.text = value; 
    [cell.contentView addSubview:label]; 
    [label release]; 
    label = nil; 


    NSLog(@"%@\t\t%@\t\t\t\t\n%@",[rowrecordTemp objectAtIndex:0] ,[rowrecordTemp objectAtIndex:1],[rowrecordTemp objectAtIndex:2]); 


} 
NSString *fontname = cell.textLabel.font.fontName; 
cell.textLabel.font = [UIFont fontWithName:fontname size:16]; 
cell.textLabel.numberOfLines = 0; 
return cell; 

}

回答

0

(section != 0)您正在向UITableViewCell添加新的UILabel。每当一个单元出队时,都会添加新的标签。您应该删除旧标签,或更改旧标签的文本而不是创建新标签。

+0

嗨Rengers。谢谢你的帮助。按照您的建议解决问题,我采取了第二种方法来更改旧标签的文本。 – 2010-04-28 03:45:48

2

表视图细胞在存储器共享。当你离队的时候,它会有一个旧的价值(我认为最后一个要离队)。您必须清除其旧内容才能显示其新内容。

如果您沿着cell.textLabel.text = [arr objectAtIndex:indexPath.row]的方向做某件事情,则没有问题,因为文本将被简单替换。如果您有自定义视图或更复杂的视图,则必须完全替换视图。

+0

嗨eman: 我同意你的意见。我正在做你建议的事情,我认为旧文本将被作业取代。但是,似乎旧文本保持不变。 – 2010-04-26 22:38:42

+0

发布您的'cellForRowAtIndexPath'代码 - 这可能是问题所在。 – shosti 2010-04-26 23:06:08

相关问题