问题描述
在编辑模式下滚动列表时,所选行的格式会在重新查看时发生变化(参见视频:https://www.youtube.com/watch?v=kKb6BrgUxL4)。
我知道我可以通过避免使用编辑模式来解决这个问题,但我想知道是否有其他解决方法仍然允许我使用编辑模式和内置的“multiSelection”功能(而不是写入我自己的按钮)。
我已经包含了基本的示例代码,对于任何想要测试的人来说,它应该很容易在新的 Xcode 项目中运行。该问题出现在 iPhone 8 和 11 的 Xcode 模拟器中,以及在 iPhone SE(第一代)上运行程序时。最突出的问题是某些行最终固定在屏幕上,这发生在上面视频中的 0:45。
import SwiftUI
struct ContentView: View {
// ---------------------------------
struct Item: Hashable,Identifiable {
let title: String
let id = UUID()
}
struct Sub: Identifiable {
let name: String
let items: [Item]
let id = UUID()
}
private let listItems: [Sub] = [
Sub(name: "List section 1",items: [Item(title: "List item 1"),Item(title: "List item 2"),Item(title: "List item 3"),Item(title: "List item 4"),Item(title: "List item 5"),Item(title: "List item 6"),Item(title: "List item 7")]),Sub(name: "List section 2",Item(title: "List item 7")])
]
@State private var multiSelection = Set<UUID>()
// ---------------------------------
@State var nTotal:Int = 0
@State var alertVisible: Bool = false
@State var resetVisible: Bool = false
func resetClicked() {
resetVisible = true
}
func resetConfirm() {
multiSelection = Set<UUID>()
}
// ---------------------------------
var body: some View{
let nTotal = listItems.reduce(0) { (current,next) -> Int in
return (current + next.items.count)}
vstack{
HStack{
Spacer()
Text("Checklist") .font(.title)
Spacer()
Button(action: resetClicked) {
Image(systemName: "exclamationmark.arrow.circlepath")
}
.padding(.trailing,15.0)
.alert(isPresented: $resetVisible) {Alert(title: Text("Are you sure?"),message: Text("All tasks will be reset."),primaryButton: Alert.Button.cancel(Text("Cancel")),secondaryButton: Alert.Button.destructive(Text("Reset"),action: resetConfirm))}
}
.padding(.top,10)
ProgressView(value: Float(multiSelection.count),total: Float(nTotal))
.alert(isPresented: $alertVisible) {Alert(title: Text("Congratulations!"),message: Text("All tasks complete! You're ready to start"),dismissButton: .default(Text("Done")))
}
.onChange(of: multiSelection.count) { newValue in
if (nTotal == multiSelection.count) {
alertVisible = true
}
}
HStack {
Text(String(multiSelection.count))
.font(.caption)
Text("/")
.font(.caption)
Text(String(nTotal))
.font(.caption)
}
// ---------------------------------
List(selection: $multiSelection) {
ForEach(listItems) { Sub in
Section(header: Text("\(Sub.name)")) {
ForEach(Sub.items) { items in
if self.multiSelection.contains(items.id) {
Text("\(items.title)").foregroundColor(.gray)
} else {
Text("\(items.title)")
}
}
}
}
}
.environment(\.editMode,.constant(EditMode.active))
.listStyle(InsetGroupedListStyle())
// ---------------------------------
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)