views:

77

answers:

1

I have a very strange problem with a UITableview within a navigation controller on the iPhone simulator. Of the cells displayed, only some are correctly rendered. They are all supposed to look the same but the majority are missing the accessory I've set, scrolling the view changes which cell has the accessory so I suspect it's some sort of cell caching happening, although the contents are correct for each cell. I also set an image as the background and that was also only displaying sporadically but I fixed that by changing

cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yellow-bar_short.png"]];

(which also only rendered a random cell with the background) to

cell.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"yellow-bar_short.png"]];

I now need to fix the problem with the accessory only showing on a random cell. I tried moving the code from cellForRowAtIndex to willDisplayCell but it made no difference. I put in a log command to confirm that it is running through each frame.

Basically it's a table view (UITableViewCellStyleSubtitle) that gets its info from a server & is then updated by a delegate method calling reload. Code is:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
  NSLog(@"%@", [NSString stringWithFormat:@"Setting colours for cell %i", indexPath.row]);

  // Set cell background
  // cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yellow-bar_short.png"]];
  cell.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"yellow-bar_short.png"]];
  cell.textLabel.backgroundColor = [UIColor clearColor];
  cell.detailTextLabel.backgroundColor = [UIColor clearColor];

      // detailTableViewAccessory is a view containing an imageview in this view's nib
      // i.e. nib <- view <- imageview <- image
  cell.accessoryView = detailTableViewAccessory;
}

// Called by data fetching object when done
-(void)listDataTransferComplete:(ArticleListParser *)articleListParserObject
{
  NSLog(@"Data parsed, reloading detail table");
  self.currentTotalResultPages = (((articleListParserObject.currentArticleCount - 1) / 10) + 1);
  self.detailTableDataSource = [articleListParserObject.returnedArray copy]; // make a local copy of the returned array

  // Render table again with returned array data (neither of these 2 fixed it)
  [self.detailTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
  // [self.detailTableView reloadData];

  // Re-enable necessary buttons (including table cells)
  letUserSelectRow = TRUE;
  [btnByName setEnabled:TRUE];
  [btnByPrice setEnabled:TRUE];

  // Remove please wait message
  NSLog(@"Removing please wait view");
  [pleaseWaitViewControllerObject.view removeFromSuperview];
}

I only included code that I thought was relevant, can supply more if needed. I can't test it on an iPhone yet so I don't know if it's maybe just a simulator anomaly or a bug in my code. I've always gotten good feedback from questions, any ideas?

A: 

Resolved! What I thought was a rendering problem was actually just a stupidity problem on my part (serves me right for blindly following a tutorial). IB was only creating one instance of the accessory view which was being assigned to one cell. Fixed it by recreating the view on each cell creation:

cell.accessoryView = detailTableViewAccessory;

replaced by

UIImageView *cellAccessoryView = [[UIImageView alloc] initWithFrame:CGRectMake(308, 0, 12, 75)];
cellAccessoryView.image = [UIImage imageNamed:@"blue-accessory-pointer.png"];
[cell.contentView addSubview:cellAccessoryView];
[cellAccessoryView release];

I should also add that the issue I had with the cell background was fixed with the same methodology. Also, in place of the addSubview line you could use

cell.accessoryView = cellAccessoryView;
Spider-Paddy