0
我有Xcode 4.3并且该类不使用ARC。滚动单元格数据更改
我创建了一个使用可重用单元的tableview。我试图从别人那里得到答案,但无法得到适当的答案。我使用UILabel
来显示数据,使用UIImageView
来显示图像。
我的代码如下。这很长,但想法是创建并放置一次,并更改数据。
在滚动中,只有一个小区改变(滚动时我看到不同的数据),以及其他的再利用细胞显示从小区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
,这是一个正确的行。
怎么了?
我该如何解决这个问题?以及我应该如何使用子类?我不想要一个单元nix,这会造成很多麻烦。 –
我管理修复它与以下链接:http://stackoverflow.com/questions/1802707/detecting-which-uibutton-was-pressed-in-a-uitableview –