How to programmatically select a row in UITableView in Swift

前端 未结 7 592
执笔经年
执笔经年 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:07

    Swift 3.x

    if you want to do it at the 'cell-creation', you can do it like this

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = TableViewCell()
        let item = items[indexPath.row]
    
        cell.textLabel?.text    = item.title
    
        if (item.checked) {
            tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
        }
    
        return cell
    }
    

提交回复
热议问题