I have Xcode 4.3 and that class doesn't use ARC.
I have created a tableview which use a reuseable cells. I have tried to get the answer from others but couldn't get a proper one.
My table gets the data from an array and I use UILabel to show the data and UIImageView to show an image.
My code is below. It's long but the idea is to create and put the UILabel, button, and image once and change the data.
On scrolling, only one cell changes (while scrolling I see different data), and the other reused cells show the same data from cell 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;
}
The data is like below: I can see 5 cells every time and when scrolling I see the behavior below:
1
2
3
4
5 - the cell 5 changes all the time while scrolling
1 - cell one data again
2
etc
When I check the indexPath.row, it is the right row.
What's wrong?