2012-02-09 50 views
1

現在我正在獲取表視圖標籤中的文本,當我更新在0索引處全局聲明的項目數組並返回時,它不會更新表索引0處的表視圖行中的行,但是當我更新其他行然後更新。而且,當我向上滾動表格視圖時,它顯示索引0在所有索引值。這裏是我的代碼在uitableview的標籤中重新加載文本

- (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];  
nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(45, 8, 100, 25)]; 
nameLabel.backgroundColor = [UIColor clearColor]; 
nameLabel.font = [UIFont fontWithName:@"Arial" size:16]; 
[cell addSubview:nameLabel]; 

deleteButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
deleteButton.frame= CGRectMake(3, 3, 30, 30); 
[deleteButton setImage: [UIImage imageNamed:@"cross_btn.png"] forState:UIControlStateNormal]; 
[cell addSubview:deleteButton]; 

priceMark = [[UILabel alloc]initWithFrame:CGRectMake(200, 8, 60, 25)]; //150, 8, 65, 25 
priceMark.backgroundColor = [UIColor clearColor]; 
priceMark.font = [UIFont fontWithName:@"Arial" size:14]; 
[cell addSubview:priceMark]; 

qtyLabel = [[UILabel alloc]initWithFrame:CGRectMake(160, 8, 65, 25)]; //225, 8, 60, 25 
qtyLabel.backgroundColor = [UIColor clearColor]; 
qtyLabel.font = [UIFont fontWithName:@"Arial" size:14]; 
[cell addSubview:qtyLabel]; 
} 
nameLabel.text = [[itemsArray objectAtIndex:indexPath.row] objectForKey:@"itemname"]; 
priceMark.text = [NSString stringWithFormat:@"Rs: %@",[[itemsArray 
objectAtIndex:indexPath.row] objectForKey:@"amount"]]; 
qtyLabel.text = [[itemsArray objectAtIndex:indexPath.row] objectForKey:@"qty"]; 

return cell; 
} 

回答

2

改變你的代碼:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
reuseIdentifier:CellIdentifier] autorelease];  
nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(45, 8, 100, 25)]; 
nameLabel.backgroundColor = [UIColor clearColor]; 
nameLabel.font = [UIFont fontWithName:@"Arial" size:16]; 
//added 
nameLabel.tag = 101; 
[cell addSubview:nameLabel]; 

deleteButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
deleteButton.frame= CGRectMake(3, 3, 30, 30); 
[deleteButton setImage: [UIImage imageNamed:@"cross_btn.png"] forState:UIControlStateNormal]; 
//added 
deleteButton.tag = 102; 
[cell addSubview:deleteButton]; 

priceMark = [[UILabel alloc]initWithFrame:CGRectMake(200, 8, 60, 25)]; //150, 8, 65, 25 
priceMark.backgroundColor = [UIColor clearColor]; 
priceMark.font = [UIFont fontWithName:@"Arial" size:14]; 
//added 
priceMark.tag = 103; 

[cell addSubview:priceMark]; 

qtyLabel = [[UILabel alloc]initWithFrame:CGRectMake(160, 8, 65, 25)]; //225, 8, 60, 25 
qtyLabel.backgroundColor = [UIColor clearColor]; 
qtyLabel.font = [UIFont fontWithName:@"Arial" size:14]; 
//added 
qtyLabel.tag = 104; 

[cell addSubview:qtyLabel]; 
} 

//Added 
nameLabel = (UILabel*)[cell viewWithTag:101]; 
deleteButton = (UIButton*)[cell viewWithTag:102]; 
priceMark = (UILabel*)[cell viewWithTag:103]; 
qtyLabel = (UILabel*)[cell viewWithTag:104]; 
// 

nameLabel.text = [[itemsArray objectAtIndex:indexPath.row] objectForKey:@"itemname"]; 
priceMark.text = [NSString stringWithFormat:@"Rs: %@",[[itemsArray 
objectAtIndex:indexPath.row] objectForKey:@"amount"]]; 
qtyLabel.text = [[itemsArray objectAtIndex:indexPath.row] objectForKey:@"qty"]; 

return cell; 
} 
+0

這是發生,你正在失去你的附加標籤/按鈕引用。通過讓他們與「標籤」你可以取回他們並更新他們的價值觀 – samfisher 2012-02-09 05:29:51