John Smith
John Smith

Reputation: 53

Xcode - Swift Programmatically select tablecell

I need to know how to set the state of a tableviewcell to selected via code.

I tried the following:

let cell:TblCell = tableView.cellForRowAtIndexPath(indexPath) as TblCell
cell.selected = false;

But this didn't work, even though it did not give any errors.

Should this have worked? or is this done differently?

Upvotes: 0

Views: 2774

Answers (1)

Matthias Bauch
Matthias Bauch

Reputation: 90117

tableView.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: .Middle)

Instead of .Middle you can use .None so the tableView doesn't scroll to the selected cell. .Top and .Bottom are also available options.

Upvotes: 6

Related Questions