Question

I'm having some trouble with a custom cell. The deal is that I want certain cells to have a button to the left of the accessory view. I achieved this by adding a button to those certain cell's subview in the cellForRowAtIndexPath-method. However this caused some trouble when reloading the cells (I have a double-tap to reload-method). Specifically what happened was that the button would flash (disappear, then appear). However, none of the content did the same.

I thought making custom cells where each cell actually have the button, but only certain cells have that button set to visible, would solve it. I tried doing this, but the problem is now that the button disappears totally when reloading. The same does also the checkmark for the accessory view if there is one. However, the correct part of the cellForRowAtIndexPath is called...

Does anyone know what is causing this?

Here's my initWithStyle for the custom cell:

self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
  // Create the button
  UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  [button setImage:[UIImage imageNamed:@"open.png"] forState:UIControlStateNormal];
  [button setFrame:CGRectMake(220, 0, 100, 86)];
  button.tag = 5;
  [self.contentView addSubview:button];  
  self.selectionStyle = UITableViewCellSelectionStyleNone; 
}
return self;

And here's the part of the cellForRowAtIndexPath that either hides the button or not:

if(item.hasImage) {
  NSLog(@"Made button visible");
  UIView *button = [cell.contentView viewWithTag:5];
  button.hidden = NO;
} else {
  NSLog(@"Made button invisible");
  UIView *button = [cell.contentView viewWithTag:5];
  button.hidden = YES;
}

Any help would be appreciated! Thanks in advance!

Was it helpful?

Solution

Ok, so I got it.

Turns out there were some problems with the z-index. I don't quite get it, but when I updated the cell, the button was actually visible, it was just under the cell. So I set the 'z-index' of the button manually, and now I don't have any problems.

Thanks to everyone that gave me feedback!

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