0

i'm having a weird issue with the code on my (static) cells. In multiple cells I have a UIButton. When I click on it, it's all fine, but when I click on the area next to the button (still in the cell itself) it turns grey (color when selected cell) + the button also disappears!

I tried to change the background of the cell to white when pressed with the following code:

    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    let myBackView=UIView(frame:cell.frame)
    myBackView.backgroundColor = UIColor.clearColor()
    cell.selectedBackgroundView = myBackView

}

but the issue is still there.When the cell itself isn't pressed This is when the static cell isn't pushed When I pressed the cell (not the button) This is when I pressed the cell (NOT the button within the cell)

SoundShock
  • 485
  • 1
  • 3
  • 24

1 Answers1

1

This is a common issue when selecting UITableViewCells, see Why do all backgrounds disappear on UITableViewCell select?

If you don't intend the cell to be selected, implement this delegate method:

- (NSIndexPath * _Nullable)tableView:(UITableView * _Nonnull)tableView willSelectRowAtIndexPath:(NSIndexPath * _Nonnull)indexPath

and return nil when indexPath is the one of your cell. This will disable selection for this cell.

If none of your cells is intended to be selected, it's even easier, just set your UITableView's allowsSelection to NO (this can also be done in Interface Builder).

Guillaume Algis
  • 10,705
  • 6
  • 44
  • 72
  • Hi, I do need some cells to be selected though. I tried the "willSelectRowAtIndexPath", but it didn't solve my problem (can't select the cells that need to work, and the buttons still disappear). Is it because I'm using 7 static cells? – SoundShock Apr 13 '16 at 17:23
  • Did you look at the first link I posted? (https://stackoverflow.com/questions/7053340/why-do-all-backgrounds-disappear-on-uitableviewcell-select) I don't think the buttons disappear, their background become transparent and as your image and text are white you don't see anything anymore. – Guillaume Algis Apr 13 '16 at 17:33
  • 1
    Hey, found a solution that worked for me: I link the static cell via IB to the code, and then just do `mailCell.selectionStyle = UITableViewCellSelectionStyle.None` This kept my button working, but the cell didn't do the whole disappear act. – SoundShock Apr 13 '16 at 17:37
  • 1
    IIRC you can also set the `selectionStyle` in Interface Builder. – Guillaume Algis Apr 13 '16 at 17:38
  • Yes Indeed, but for some reason I prefer to have it in code as well :D but thanks for the help, it made me look in the right direction – SoundShock Apr 13 '16 at 17:46