NSMutablearray move object from index to index

前端 未结 7 915
无人共我
无人共我 2020-12-05 04:05

I have a UItableview with reordable rows and the data is in an NSarray. So how do I move an object in the NSMutablearray when the appropriate tableview delegate is called?

相关标签:
7条回答
  • 2020-12-05 04:32

    With Swift's Array it's as easy as this:

    Swift 3

    extension Array {
        mutating func move(at oldIndex: Int, to newIndex: Int) {
            self.insert(self.remove(at: oldIndex), at: newIndex)
        }
    }
    

    Swift 2

    extension Array {
        mutating func moveItem(fromIndex oldIndex: Index, toIndex newIndex: Index) {
            insert(removeAtIndex(oldIndex), atIndex: newIndex)
        }
    }
    
    0 讨论(0)
提交回复
热议问题