0

我有 Xcode 4.3 并且该类不使用 ARC。

我创建了一个使用可重用单元格的表格视图。我试图从其他人那里得到答案,但找不到合适的答案。

我的表从数组中获取数据,我用它UILabel来显示数据和UIImageView显示图像。

我的代码如下。它很长,但想法是创建并放置UILabel一次,按钮和图像并更改数据。

滚动时,只有一个单元格发生变化(滚动时我看到不同的数据),而其他重复使用的单元格显示来自单元格 1 的相同数据。

static NSString * CellIdentifier = @"HistoryCellId";

iImage = [UIImage imageNamed: [NSString stringWithFormat:@"%@.png",[(_searching ? _titleArrCopy : _titleArr) objectAtIndex:indexPath.row]]];

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                  reuseIdentifier:CellIdentifier];
    iconImage = [[UIImageView alloc] init];
    UIImage * image = [UIImage imageNamed:@"HFcellBG.png"];
    UIImageView * bgImage = [[UIImageView alloc] initWithImage: image];
    cell.backgroundView = bgImage;
    //[iconImage removeFromSuperview];
    iconImage.image = iImage;
    iconImage.frame = CGRectMake(_iconX, 11, 58, 58);
    //iconImage.backgroundColor = [UIColor whiteColor];
    [cell addSubview:iconImage]; // show the image on the side

    titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(_titleX, 3, 212, 25)];
    titleLabel.textAlignment = _nibNameRTL ? UITextAlignmentRight : UITextAlignmentLeft;
    [cell addSubview:titleLabel]; // show a title

    timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(_dateX, 25, 212, 20)];
    timeLabel.textAlignment = _nibNameRTL ? UITextAlignmentRight : UITextAlignmentLeft;
    timeLabel.font = [UIFont systemFontOfSize:15]; // show another data
    [cell addSubview:timeLabel];

    favoriteBtn = [[UIButton alloc] initWithFrame:CGRectMake(_removeX, 48, 66, 23)];
    favoriteBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [favoriteBtn setTitle:getTextValue(3) forState:UIControlStateNormal];
    [cell addSubview:favoriteBtn];
     // show a button

} 

[iconImage setImage:iImage];

[titleLabel setText:[self parserData:indexPath.row]];

[timeLabel setText:[NSString stringWithFormat:getTextValue(4),[(_searching ? _TimeArrCopy :_TimeArr) objectAtIndex:indexPath.row]]];

if ([[(_searching ? _FavoriteArrCopy : _FavoriteArr) objectAtIndex:indexPath.row] isEqualToString:@"1"]) {
    [favoriteBtn setEnabled:NO];
    [favoriteBtn setBackgroundImage:[UIImage imageNamed:@"HFfavoriteCheckedBtn.png"] forState:UIControlStateNormal];
}
else {
    [favoriteBtn setEnabled:YES];
    [favoriteBtn setBackgroundImage:[UIImage imageNamed:@"HFfavoriteBtn.png"] forState:UIControlStateNormal];
}

favoriteBtn.tag = indexPath.row;
[favoriteBtn addTarget:self action:@selector(addToFavorite:) 
      forControlEvents:UIControlEventTouchUpInside];


//    [cell.favoriteBtn setTitle:getTextValue(3) forState:UIControlStateNormal];
//    [cell.typeImage setImage:iconImage];
//    cell.data = [_dataArr objectAtIndex:indexPath.row];

cell.selectionStyle = UITableViewCellSelectionStyleNone;
// return it
return cell;

}

数据如下:我每次可以看到 5 个单元格,滚动时看到以下行为:

1
2
3
4
5 - the cell 5 changes all the time while scrolling
1 - cell one data again
2
etc

当我检查时indexPath.row,它是正确的行。

怎么了?

4

1 回答 1

0

您尝试用于配置单元格的变量在 if 检查中初始化,该检查很可能不是真的(尤其是在滚动之后,当您的单元格实例已经存在时)。您应该访问单元对象的属性来修改它的状态。

这是 Objective-C 允许您向 nil 发送消息的缺陷之一。

[titleLabel setText:[self parserData:indexPath.row]];

滚动后titleLabel为nil,不会报错。

如果您的单元格中有很多自定义 UI,我建议创建一个自定义子类,并为您需要自定义的所有子视图添加属性,然后在 if(cell == nil) 块之外使用这些属性。

于 2012-06-18T18:50:34.173 回答