在tableview标签中的以前的文本显示后重新加载tableview

问题描述:

我有一个全局声明的数组,并在表视图中加载该数组,但是当我去其他控制器和更新该数组并重新加载表视图再在表视图中的标签中的文本单元格覆盖。较新的更新数据覆盖并且以前也以相同的标签显示。这里是我的代码:其中项目数组是全局声明数组。在tableview标签中的以前的文本显示后重新加载tableview

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath 
*)indexPath { 

static NSString *CellIdentifier = @"Cell"; 


UITableViewCell *cell = [self.tableview dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
reuseIdentifier:CellIdentifier] autorelease];  
    } 

UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(45, 8, 100, 25)]; 
nameLabel.backgroundColor = [UIColor clearColor]; 
nameLabel.font = [UIFont fontWithName:@"Arial" size:16]; 
nameLabel.text = [[itemsArray objectAtIndex:indexPath.row] objectForKey:@"itemname"]; 
[cell addSubview:nameLabel]; 
[nameLabel release]; 

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


UILabel *priceMark = [[UILabel alloc]initWithFrame:CGRectMake(150, 8, 65, 25)]; 
priceMark.backgroundColor = [UIColor clearColor]; 
priceMark.font = [UIFont fontWithName:@"Arial" size:14]; 
priceMark.text = [NSString stringWithFormat:@"Rs: %@",[[itemsArray objectAtIndex:indexPath.row] 
objectForKey:@"amount"]]; 
[cell addSubview:priceMark];  
[priceMark release]; 

UILabel *qtyLabel = [[UILabel alloc]initWithFrame:CGRectMake(225, 8, 60, 25)]; 
qtyLabel.backgroundColor = [UIColor clearColor]; 
qtyLabel.font = [UIFont fontWithName:@"Arial" size:14]; 
qtyLabel.text = [[itemsArray objectAtIndex:indexPath.row] objectForKey:@"qty"]; 
[cell addSubview:qtyLabel]; 
[qtyLabel release]; 

return cell; 
} 

EDIT 2

现在我得到另一个问题,当我更新是全球在0指数声明项数组,并返回然后它不会在索引0更新表视图鳞次栉比,但当我更新其他行时,它会更新。而且,当我向上滚动表格视图时,它显示索引0在所有索引值。这里是我的代码

- (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; 
    } 

你不应该创建新的UILabels和UIButtons并将它们作为子视图添加每次。 这里你的问题是你每次都添加新的。 你应该用标签和按钮制作一个自定义的UITableViewCell并设置它们。

+0

哦!我怎么能做这个愚蠢的错误..谢谢! :) – Ketan 2012-02-08 13:26:26

+0

Nikola Kirev,我面临着同样的问题,但很少有数据是动态的,必须根据数据添加ImageView。那么在这种情况下应该怎么做? – Sandy 2014-03-28 12:26:08