简体   繁体   English

如何使用SwiftUI框架实现删除和移动功能

[英]How to implement delete and move function using SwiftUI framework

Not able to delete and move row at for the item in the list view. 无法在列表视图中删除该项目并将其移至该行。

Apple provided in their training video these methods but its not working 苹果在他们的培训视频中提供了这些方法,但无法正常工作

For removing: 删除:

array.remove(atOffsets: IndexSet)

For moving: 移动:

array.move(fromOffsets: IndexSet, toOffset: Int)

Both of these method are not available in the apple documentation. Apple文档中没有这两种方法。

模拟器画面

the methods you are refering to are now available on Xcode 11 GM. Xcode 11 GM现在提供了您所引用的方法。 You can use them like this. 您可以像这样使用它们。

removing an element 删除元素

TLDR: TLDR:

.onDelete{offsets in
            self.array.remove(atOffsets: offsets)

full code (can copy paste): import SwiftUI 完整代码(可以复制粘贴):导入SwiftUI

struct MyTableView : View {

@State var array=[1,2,3,4,5]

var body: some View {

    List{
        ForEach(array,id:\.self){element in
            Text("\(element)")
        }
        .onDelete{offsets in
            self.array.remove(atOffsets: offsets)

        }
    }

    }
}


struct MyTableView_Previews : PreviewProvider {
    static var previews: some View {
        MyTableView()
    }
}

moving elements 运动元素

.onMove { (offsets, targetOffset) in
            self.array.move(fromOffsets: offsets, toOffset: targetOffset)
        }

These methods are part of the Swift 5.1 standard library, which Apple will make available with a future beta of Xcode. 这些方法是Swift 5.1标准库的一部分,Apple会在以后的Xcode Beta中提供该方法。 In the meantime, you can use these extensions: 同时,您可以使用以下扩展名:

extension Array {
    mutating func remove(atOffsets offsets: IndexSet) {
        let suffixStart = halfStablePartition { index, _ in
            return offsets.contains(index)
        }
        removeSubrange(suffixStart...)
    }

    mutating func move(fromOffsets source: IndexSet, toOffset destination: Int) {
        let suffixStart = halfStablePartition { index, _ in
            return source.contains(index)
        }
        let suffix = self[suffixStart...]
        removeSubrange(suffixStart...)
        insert(contentsOf: suffix, at: destination)
    }

    mutating func halfStablePartition(isSuffixElement predicate: (Index, Element) -> Bool) -> Index {
        guard var i = firstIndex(where: predicate) else {
            return endIndex
        }

        var j = index(after: i)
        while j != endIndex {
            if !predicate(j, self[j]) {
                swapAt(i, j)
                formIndex(after: &i)
            }
            formIndex(after: &j)
        }
        return i
    }

    func firstIndex(where predicate: (Index, Element) -> Bool) -> Index? {
        for (index, element) in self.enumerated() {
            if predicate(index, element) {
                return index
            }
        }
        return nil
    }
}

They either used custom extensions to Array or some version of Swift not available to the public yet. 他们使用了Array的自定义扩展或尚未公开的某些版本的Swift。 Workarounds that I implemented are: 我实现的解决方法是:

delete 删除


func delete(at offsets: IndexSet) {
    if let first = offsets.first {
        store.rooms.remove(at: first)
    }
}

move 移动


func move(from source: IndexSet, to destination: Int) {
    if let first = source.first {
        store.rooms.swapAt(first, destination)
    }
}

Move function works however, the animation is not as good as the one on video at WWDC 2019. 移动功能有效,但是动画效果不如WWDC 2019上的视频动画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM