我在使用dequeueReusableCellWithIdentifier
方法時遇到問題。每當我使用這個單元格時,一個單元格會顯示自己的值和不屬於它的值。這first和second圖像顯示的問題,而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;
}
嗨Rengers。謝謝你的幫助。按照您的建議解決問題,我採取了第二種方法來更改舊標籤的文本。 – 2010-04-28 03:45:48