How to programmatically select a row in UITableView in Swift

前端 未结 7 587
执笔经年
执笔经年 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-24 05:17

    Swift 4.2:

    Select one or more Rows

    let ndx:IndexSet = [1]
    // or:  let ndx:IndexSet = [1, 2, 3]; // etc
    tableview?.selectRowIndexes(ndx, byExtendingSelection: false);
    

    Deselect a Single Row

    tableview?.deselectRow(current)
    

    Note that if you have (func tableViewSelectionDidChange(...)) implemented, that will be triggered by the above.

    Also see Charlton Provatas' answer at https://stackoverflow.com/a/48696458/352920 for an extension to NSTableView, that provides a simple

    tableview?.selectRow(at:) 
    

提交回复
热议问题