Question

I am currently having a problem with a UITableView, and more precisely with a Custom Cell.

I got a table view controller, let's say ControllerA, which is in charge of displaying different TableViewCells. These cells are custom cells, defined in another class, let's say ControllerCell. Each cell contains one of these info buttons (small, round with "i"). These buttons are only displayed when there's something to show.

In ControllerCell, I define what's next :

@property (nonatomic, retain) IBOutlet UIButton *infoButton;
@property (nonatomic, retain) IBOutlet UIAlertView *alert;
@property (nonatomic, retain) IBOutlet NSString *info;
- (IBAction)infoSupButton:(id)sender;

as well as the @synthesis, just like everybody would do that. Then I define what's going to happen with the alert :

- (IBAction)infoSupButton:(id)sender {
        alert = [[UIAlertView alloc] initWithTitle:@"Informations supplémentaires" 
                                                    message:info
                                                   delegate:self 
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
       if (info != nil) {
      [alert show];
      }
}

And in the "initWithStyle" section, I do

[_infoButton addTarget:self action:@selector(infoSupButton:) forControlEvents:UIControlEventTouchUpInside];

That was for the declaration of the cell.

Now let's focus on ControllerA.

I'm parsing a XML file to get the "info" data, which should be displayed when one clicks on the "infoButton". This is not really a problem, because I can get this data and show it in the console.

Once the data is parsed, i fill a NSMutableArray, in the viewDidLoad section :

tableInfoSup = [[NSMutableArray alloc] init];

Then follow the classic methods :

-numberOfSectionsInTableView: (3 sections) -numberOfRowsInSection: (8 rows in section 0, 4 in section 1 and 4 in section 2) -cellForRowAtIndexPath:

I got 3 different sections, displaying cells with different information, and in the cellForRowAtIndex method, I do :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    

static NSString *cellIdentifier = @"ExerciceTableCell";
ControllerCell *cell = (ControllerCell *)[tableView dequeueReusableCellWithIdentifier:exerciceTableIdentifier];

if (cell == nil) 
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ExerciceTableCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];

} 


if (indexPath.section == 0) {
    [...]


    if ([[tableInfoSup objectAtIndex:indexPath.row] isEqualToString:@""]) {
        [cell.infoButton setHidden:YES];
        cell.info = nil;
    }
    else {
        cell.info = [tableInfoSup objectAtIndex:indexPath.row];
    }

}
if (indexPath.section == 1) {
    [...]

    if ([[tableInfoSup objectAtIndex:indexPath.row+8] isEqualToString:@""]) {
        [cell.infoButton setHidden:YES];
        cell.info = nil;
    }
    else {
        cell.info = [tableInfoSup objectAtIndex:indexPath.row+8]; //got the 8 rows from section 0;
    }

}
if (indexPath.section == 2) {

    [...]

    if ([[tableInfoSup objectAtIndex:indexPath.row+12] isEqualToString:@""]) {
        [cell.infoButton setHidden:YES];
        cell.info = nil;
    }
    else {
        cell.info = [tableInfoSup objectAtIndex:indexPath.row+12];  //got the 8 rows from section 0, + 4 rows from section 1
    }    
}


return cell;
}

Now, the problem is that, when the screen is displayed for the 1st time, everything is in oder : I got cells with the small "i" button, displaying the good UIAlertView, some other cells do not display the button...That's normal. But after several scrollings, the "i" buttons start to disappear....I don't know why.

Has anybody an idea? Thanks :-)

Was it helpful?

Solution

In your tableView:cellForRowAtIndexPath:, you hide the info when you don't want it to be shown, but you don't explicitly unhide it for cells where it should be shown.

Look at the first two lines in that method: What you are - correctly - doing is reusing your cells, so when cells are scrolled out of view, they are removed from the UITableView and put into the reuse queue. Then, when cells should become visible, the TableView gets cells from that queue - or creates new ones if none are available.

This all goes very well, but after a while, cells with hidden info buttons are put on the queue. And then, some time later, those cells are reused - and sometimes for rows in which there should be info visible.

There are two solutions to this: You could either explicitly unhide the information for those rows where you want it to be shown, or you could use two different kinds of cell, one with hidden info, and one with visible info. You then give each of those cells a different identifier, and based on what row the cells are in, set the identifier before dequeuing/creating cells.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top