2

在我的应用程序中,我有一个 imageView 和一个 UILabel。对于 ImageView,我已经为它分配了异步图像,它工作正常,但是当我滚动表格时,第一行和最后一行的标签会重叠。

这是我的代码:

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

    productObject=[appDelegate.productArray objectAtIndex:indexPath.row];
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UILabel *nameLabel;
    if (cell == nil) 
    {

        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero     reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;

    }   

    else 
    {

        AsyncImageView* oldImage = (AsyncImageView*)[cell.contentView viewWithTag:999];
        [oldImage removeFromSuperview];
    }

    CGRect nameLabelRect = CGRectMake(70,80,150,15);
    nameLabel = [[UILabel alloc] initWithFrame:nameLabelRect];    
    nameLabel.text = [NSString stringWithFormat:@"%@",productObject.productname];
    nameLabel.textAlignment = UITextAlignmentCenter;
    nameLabel.lineBreakMode = UILineBreakModeCharacterWrap;
    nameLabel.font = [UIFont boldSystemFontOfSize:15];
    nameLabel.backgroundColor = [UIColor clearColor];
    [cell.contentView addSubview: nameLabel];

    }
4

2 回答 2

7

对于每个单元格使用单独的标识符,如下所示

 NSString *cellIdentifier = [NSString stringWithFormat:@"cell%i",indexpath.row];

或者只是不使用可重用性,只需将单元格和数据放入其中

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

UITableViewCell *cell = [[UITableViewCell alloc]init];
UIButton *favBtn = [UIButton buttonWithType:UIButtonTypeCustom];
favBtn.frame = CGRectMake(0, 0, 50, 50);
favBtn.backgroundColor = [UIColor clearColor];
favBtn.showsTouchWhenHighlighted = YES;
favBtn.tag = indexPath.row;

UIButton *arrowBtn = [UIButton buttonWithType:UIButtonTypeCustom];
arrowBtn.frame = CGRectMake(280, 26, 25, 25);
arrowBtn.backgroundColor = [UIColor clearColor];
arrowBtn.showsTouchWhenHighlighted = YES;
[arrowBtn setImage:[UIImage imageNamed:@"arrow.png"] forState:UIControlStateNormal];

UILabel *date = [[UILabel alloc]initWithFrame:CGRectMake(55, 26, 250, 25)];
date.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:0]  size:15];
date.text = ann.title;
date.textColor = [UIColor blackColor];
date.backgroundColor = [UIColor clearColor];

[cell.contentView addSubview:favBtn];
[cell.contentView addSubview:date];
[cell.contentView addSubview:arrowBtn];


    return cell;
}

我不是专家,但第二个对我很有用,希望你也能帮到你

于 2012-05-10T14:09:07.670 回答
0

您将 nameLabel 添加到单元格中,无论它是新创建的还是重复使用的。所以,是的,每次你将一个重复使用的单元格滚动到视图中时,它都会堆积在另一个标签上。

于 2012-05-10T14:05:48.323 回答