我有一个全局声明的数组并在表视图中加载该数组但是当我转到其他控制器并更新该数组并再次重新加载表视图时,表视图单元格中的标签中的文本将被覆盖。较新的更新数据将覆盖,之前也会显示在同一标签中。这是我的代码:其中item数组是全局声明的数组。
- (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的表视图行中的行,但是当我更新其他行然后它更新。而且当我向上滚动表格视图时,它会在所有索引上显示索引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;
}
答案 0 :(得分:3)
您不应每次都创建新的UILabel和UIButtons并将其添加为子视图。 这里的问题是你每次都要添加新的。 您应该使用标签和按钮制作自定义UITableViewCell并进行设置。