Swift - Reorder UITableView cells

前端 未结 4 1423
悲哀的现实
悲哀的现实 2021-01-04 12:53

I do know that it\'s not too hard to do it in objective C , the problem is I\'m learning Swift by skipping Objective C.

https://developer.apple.com/library/ios/docu

4条回答
  •  失恋的感觉
    2021-01-04 13:23

    All the same rules apply as in Objective-C. You set the table view data source and delegate just like you would in Objective-C.

    func tableView(tableView: UITableView!, canMoveRowAtIndexPath indexPath: NSIndexPath!) -> Bool {
        return true // Yes, the table view can be reordered
    }
    
    func tableView(tableView: UITableView!, moveRowAtIndexPath fromIndexPath: NSIndexPath!, toIndexPath: NSIndexPath!) {
        // update the item in my data source by first removing at the from index, then inserting at the to index.
        let item = items[fromIndexPath.row]
        items.removeAtIndex(fromIndexPath.row)
        items.insert(item, atIndex: toIndexPath.row)
    }
    

    If you need finer grain control, you can also implement

    func tableView(tableView: UITableView!, targetIndexPathForMoveFromRowAtIndexPath sourceIndexPath: NSIndexPath!, toProposedIndexPath proposedDestinationIndexPath: NSIndexPath!) -> NSIndexPath! {
        …
    }
    

提交回复
热议问题