How to programmatically select a row in UITableView in Swift

前端 未结 7 612
执笔经年
执笔经年 2020-12-24 04:52

I need to select a row in a UITableView programmatically using Swift 1.2.

This is the simple code:

var index = NSIndexPath(forRow: 0, inSection: 0)
         


        
7条回答
  •  清歌不尽
    2020-12-24 05:18

    Use below code,after loading your table view with data:

    let rowToSelect:NSIndexPath = NSIndexPath(forRow: 0, inSection: 0);  //slecting 0th row with 0th section
    self.tableView.selectRowAtIndexPath(rowToSelect, animated: true, scrollPosition: UITableViewScrollPosition.None);
    

    now,you have to manually call didSelectRowAtIndexPath: delegate method using below code:

    self.tableView(self.tableView, didSelectRowAtIndexPath: rowToSelect); //Manually trigger the row to select
    

    Thanks.

提交回复
热议问题