2010-04-26 59 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

相關問題