Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell Selected Background Color on Multiple Selection

// Doesn't work cell.selectionStyle = .Blue //Works when the selection is not multiple, if it's multiple with each selection the previous one disappear... let cellBGView = UIView() cellBGView.backgroundColor = UIColor(red: 0, green: 0, blue: 200, alpha: 0.4) cell.selectedBackgroundView = cellBGView 

Any answer how to set background color of the cells which are selected?

like image 424
Bogdan Bogdanov Avatar asked Nov 12 '14 19:11

Bogdan Bogdanov


1 Answers

All the above answers are fine but a bit to complex to my liking. The simplest way to do it is to put some code in the cellForRowAtIndexPath. That way you never have to worry about changing the color when the cell is deselected.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {     let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)      /* this is where the magic happens, create a UIView and set its        backgroundColor to what ever color you like then set the cell's        selectedBackgroundView to your created View */      let backgroundView = UIView()     backgroundView.backgroundColor = YOUR_COLOR_HERE     cell.selectedBackgroundView = backgroundView     return cell } 
like image 184
OverD Avatar answered Sep 22 '22 04:09

OverD