Xcode - Swift 以编程方式选择表格单元格

Joh*_*ith 0 set cell selected swift

我需要知道如何通过代码将 tableviewcell 的状态设置为选中状态。

我尝试了以下方法:

let cell:TblCell = tableView.cellForRowAtIndexPath(indexPath) as TblCell
cell.selected = false;
Run Code Online (Sandbox Code Playgroud)

但这不起作用,即使它没有给出任何错误。

这应该有效吗?还是这样做不同?

Mat*_*uch 6

tableView.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: .Middle)
Run Code Online (Sandbox Code Playgroud)

而不是.Middle您可以使用,.None因此 tableView 不会滚动到选定的单元格。.Top并且.Bottom也是可用的选项。