iphone - 重新加载表格 View 后,表格 View 中标签中的先前文本正在显示

标签 iphone ios

我有一个全局声明的数组并在表格 View 中加载该数组但是当我转到其他 Controller 并更新该数组并再次重新加载表格 View 时,表格 View 单元格中标签中的文本将被覆盖。较新的更新数据是最重要的,以前的数据也显示在同一标签中。这是我的代码:其中项目数组是全局声明的数组。

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

编辑 2

现在,当我更新在 0 索引处全局声明的项目数组并返回时,我遇到了其他问题,然后它不会更新索引 0 处的 TableView 行中的行,但是当我更新其他行时,它会更新。而且当我向上滚动表格 View 时,它会在所有索引的索引 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 并将它们添加为 subview 。
您的问题是您每次都添加新的。
您应该使用标签和按钮制作一个自定义 UITableViewCell 并设置它们。

关于iphone - 重新加载表格 View 后,表格 View 中标签中的先前文本正在显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9193512/

相关文章:

ios - 如何从类对象列表构造一个 json 对象

ios - 从手势委托(delegate)传递 Set<UITouch>

ios - Google+ iOS SDK - 获取关注 "me"的人列表

ios - 自定义应用程序中的 UITableview 部分

javascript - 如何从 fetch 中获取 JSON 数据(react-native)

ios - 在 iOS 中,我可以跳转到另一个应用程序并使其使用帐户和密码自动登录吗?

android - 为移动应用程序选择数据库的最佳实践

iphone - 调用 Objective C 中的协议(protocol)方法

iphone - 如何启动有关设备类型的特定 View

iphone - 如何从NSArray获取声音文件名并播放?