Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a UITableViewCell appear disabled?

I know about UITableview: How to Disable Selection for Some Rows but Not Others and cell.selectionStyle = UITableViewCellSelectionStyleNone, but how do I make a cell (or any UIView for that matter) appear disabled (grayed-out) like below?

disabled UITableViewCell

like image 608
ma11hew28 Avatar asked May 06 '11 00:05

ma11hew28


People also ask

How do I turn off cell selection?

You need to set the selectionStyle property on the cell: cell. selectionStyle = UITableViewCellSelectionStyle. None in Swift, or cell.


2 Answers

You can just disable the cell's text fields to gray them out:

Swift 4.x

cell!.isUserInteractionEnabled = false cell!.textLabel!.isEnabled = false cell!.detailTextLabel!.isEnabled = false 
like image 98
Symmetric Avatar answered Sep 28 '22 07:09

Symmetric


A Swift extension that works well in the context I'm using it; your mileage may vary.

Swift 2.x

extension UITableViewCell {     func enable(on: Bool) {         for view in contentView.subviews as! [UIView] {             view.userInteractionEnabled = on             view.alpha = on ? 1 : 0.5         }     } } 

Swift 3:

extension UITableViewCell {     func enable(on: Bool) {         for view in contentView.subviews {             view.isUserInteractionEnabled = on             view.alpha = on ? 1 : 0.5         }     } } 

Now it's just a matter of calling myCell.enable(truthValue).

like image 38
Kevin Owens Avatar answered Sep 28 '22 06:09

Kevin Owens