An interesting snippet discovered at stackoverflow link

This enables implementing auto-lock feature like that seen in settings -> general.


override func viewDidLoad() { super.viewDidLoad() // … tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "categoryCell") } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("categoryCell", forIndexPath: indexPath) as UITableViewCell cell.accessoryType = (lastSelectedIndexPath?.row == indexPath.row) ? .Checkmark : .None cell.textLabel?.text = categories[indexPath.row] return cell } func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { tableView.deselectRowAtIndexPath(indexPath, animated: true) if indexPath.row != lastSelectedIndexPath?.row { if let lastSelectedIndexPath = lastSelectedIndexPath { let oldCell = tableView.cellForRowAtIndexPath(lastSelectedIndexPath) oldCell?.accessoryType = .None } let newCell = tableView.cellForRowAtIndexPath(indexPath) newCell?.accessoryType = .Checkmark lastSelectedIndexPath = indexPath } }

LEAVE A REPLY

Please enter your comment!
Please enter your name here