我有 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
,它是正确的行。
怎么了?