私は Xcode 4.3 を持っていますが、そのクラスは ARC を使用していません。
再利用可能なセルを使用するテーブルビューを作成しました。私は他の人から答えを得ようとしましたが、適切な答えを得ることができませんでした。
私のテーブルは配列からデータを取得し、データを表示して画像を表示するために使用UILabel
します。UIImageView
私のコードは以下です。UILabel
長いですが、 、 ボタン、画像を一度作成して配置し、データを変更するという考え方です。
スクロールすると、1 つのセルのみが変更され (スクロール中に別のデータが表示されます)、他の再利用されたセルにはセル 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
と、右の行です。
どうしたの?