0

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?

NDM
  • 944
  • 9
  • 30

1 Answers1

0

The variables you are trying to use to configure the cell are initialized inside an if check that is most likely not true (especially after scrolling, when your cell instance will already exist). You should access properties of the cell object to modify it's state.

This is one of the pitfalls of Objective-C allowing you to send messages to nil.

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

titleLabel will be nil after you scroll, and there will not be an error.

If you have a lot of custom UI in your cell, I suggest making a custom subclass, and adding properties for all of the subviews which you need to customize, then use these properties outside of your if(cell == nil) block.

Chris Trahey
  • 18,202
  • 1
  • 42
  • 55
  • How can I fix this? and how should I use a subclass? I don't want a cell nix, it makes a lot of troubles. – NDM Jun 20 '12 at 17:05
  • I have managed fix it with the following link: http://stackoverflow.com/questions/1802707/detecting-which-uibutton-was-pressed-in-a-uitableview – NDM Jul 19 '12 at 19:22