拖放以在列表 SwiftUI 中移动行
Drag and drop to move row in List SwiftUI
我在 SwiftUI 中使用 List。我想在列表中添加拖放行(项目)的功能。在 Swift 中,我们有 UITableView 的委托和数据源方法,如下所示:
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
SwiftUI中这些方法的替代方案是什么?
struct User {
var firstName: String
var lastName: String
}
struct UserRow: View {
var user: User
var body: some View {
Text("\(user.firstName) \(user.lastName)")
}
}
struct ContentView : View {
var body: some View {
let user1 = User(firstName: "Anjali", lastName: "User1")
let user2 = User(firstName: "XYZ", lastName: "User2")
return List {
UserRow(user: user1)
UserRow(user: user2)
}
}
}
在 WWDC 中有一段视频简要说明了如何在列表上激活编辑模式以及如何移动项目。
您可以使用 onMove
修饰符处理单元格重新排序,并使用 EditButton()
激活编辑模式,这样您就可以手动移动单元格。
请注意,您不能对静态列表使用onMove
方法。此修饰符在 DynamicViewContent
协议中可用,该协议由 ForEach
实现,但不由 List
.
实现
struct ContentView : View {
let users = [
User(firstName: "Anjali", lastName: "User1"),
User(firstName: "XYZ", lastName: "User2")
]
var body: some View {
NavigationView {
List {
ForEach(users.identified(by: \.firstName)) { user in
UserRow(user: user)
}.onMove(perform: move)
}
.navigationBarTitle(Text("Users"))
.navigationBarItems(trailing: EditButton())
}
}
func move(from source: IndexSet, to destination: Int) {
users.move(fromOffsets: source, toOffset: destination)
}
}
要使其在没有 EditButton
的情况下始终可拖动,只需将此修饰符添加到您的列表中:
.environment(\.editMode, Binding.constant(EditMode.active))
我在 SwiftUI 中使用 List。我想在列表中添加拖放行(项目)的功能。在 Swift 中,我们有 UITableView 的委托和数据源方法,如下所示:
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
SwiftUI中这些方法的替代方案是什么?
struct User {
var firstName: String
var lastName: String
}
struct UserRow: View {
var user: User
var body: some View {
Text("\(user.firstName) \(user.lastName)")
}
}
struct ContentView : View {
var body: some View {
let user1 = User(firstName: "Anjali", lastName: "User1")
let user2 = User(firstName: "XYZ", lastName: "User2")
return List {
UserRow(user: user1)
UserRow(user: user2)
}
}
}
在 WWDC 中有一段视频简要说明了如何在列表上激活编辑模式以及如何移动项目。
您可以使用 onMove
修饰符处理单元格重新排序,并使用 EditButton()
激活编辑模式,这样您就可以手动移动单元格。
请注意,您不能对静态列表使用onMove
方法。此修饰符在 DynamicViewContent
协议中可用,该协议由 ForEach
实现,但不由 List
.
struct ContentView : View {
let users = [
User(firstName: "Anjali", lastName: "User1"),
User(firstName: "XYZ", lastName: "User2")
]
var body: some View {
NavigationView {
List {
ForEach(users.identified(by: \.firstName)) { user in
UserRow(user: user)
}.onMove(perform: move)
}
.navigationBarTitle(Text("Users"))
.navigationBarItems(trailing: EditButton())
}
}
func move(from source: IndexSet, to destination: Int) {
users.move(fromOffsets: source, toOffset: destination)
}
}
要使其在没有 EditButton
的情况下始终可拖动,只需将此修饰符添加到您的列表中:
.environment(\.editMode, Binding.constant(EditMode.active))