【问题标题】:iphone:UItableviewcell duplicates when using table reloadiphone:使用表重新加载时UItableviewcell重复
【发布时间】:2012-03-27 12:07:58
【问题描述】:

当我使用 [tableview reloadData] 时,我的 tableview 被重复了。当我搬回来时,我用它来刷新 tableview。我怎样才能克服这个问题?

这是我的代码。

#define kMyTag 1
#define despTag 2
#define subTag 3

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableview reloadData];
}    
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AsyncImageView *asyncImageView = nil;
    UILabel *label = nil;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] ;

        UILabel *mainLabel=[[UILabel alloc]init];
        mainLabel.tag = kMyTag; // define kMyTag in your header file using #define
        mainLabel.frame=CGRectMake(95, 0, 170, 30);
        mainLabel.font = [UIFont boldSystemFontOfSize:14];
        mainLabel.backgroundColor=[UIColor clearColor];
        [cell.contentView addSubview:mainLabel]; 

        UILabel *despLabel=[[UILabel alloc]init];
        despLabel.tag=despTag;
        despLabel.frame=CGRectMake(95, 25, 190, 20);
        despLabel.font= [UIFont systemFontOfSize:12];
        despLabel.backgroundColor=[UIColor clearColor];
        [cell.contentView addSubview:despLabel]; 

    } else {

        label = (UILabel *) [cell.contentView viewWithTag:LABEL_TAG];
    }
    switch([indexPath section]){
        case 0:
            { 
            NSString *urlString = [img objectAtIndex:indexPath.row];
            urlString=[urlString stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

            tname=[mname objectAtIndex:indexPath.row];
            tprice=[mprice objectAtIndex:indexPath.row];
            tquan=[mquan objectAtIndex:indexPath.row];
            tspice=[mspice objectAtIndex:indexPath.row]; 

            UILabel *mainLabel=(UILabel *)[cell.contentView viewWithTag: kMyTag];
            mainLabel.text =  [NSString stringWithFormat:@"%@",tname];
            UILabel *despLabel=(UILabel *)[cell.contentView viewWithTag: despTag];
            despLabel.text = [NSString stringWithFormat:@"Qty :%@   Spice: %@",tquan,tspice];
            UILabel *subLabel=(UILabel *)[cell.contentView viewWithTag: subTag];
            float pric=[tprice floatValue];
            subLabel.text =  [NSString stringWithFormat:@"Price :%@ %.2f",currencyType, pric];
        }
    }
}

【问题讨论】:

    标签: iphone objective-c ios uitableview reload


    【解决方案1】:

    通过从数组中删除所有对象来克服问题,然后重新选择数据库表并重新加载 uitableview。

    【讨论】:

      【解决方案2】:

      发生这种情况是因为您没有在 dequeueReusableCellWithIdentifier 中传递任何值 传递一个字符串值,然后在reuseIdentifier也使用它

      【讨论】:

      • 当我将值传递给 dequeueReusableCellWithIdentifier 时,单元格值被覆盖。
      • 尝试将开关条件放入if (cell == nil)
      • 我在 if(cell == nil) 中设置了 switch 条件,但覆盖问题仍在继续
      • 好的,现在你把 switch 条件放在 if(cell==nil) 那么你不必使用这一行
      • UILabel *mainLabel=(UILabel *)[cell.contentView viewWithTag: kMyTag];
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多