当用户单击列表中的自定义单元格/行菜单时,我试图显示弹出窗口。但每次单元格获取操作并导航到详细信息屏幕内部时。这是我的代码片段。还附上截图供参考。
var body: some View {
NavigationStack {
ZStack {
List {
Image("HeaderImage")
.resizable()
.frame(height: 150)
.listRowInsets(EdgeInsets())
ForEach((0..<8), id: \.self) { index in
NavigationLink(destination: Text("screen1")) {
CustomCellView()
}
NavigationLink(destination: Text("screen2")) {
CustomCellView(imageName: "paintpalette")
}
NavigationLink(destination: Text("screen3")) {
CustomCellView(imageName: "slider.horizontal.3")
}
}
}
.listStyle(.plain)
.navigationBarTitle("Users")
}
}
}
.listRowInsets(EdgeInsets(top: 0, leading: 16, bottom: 0, trailing: -8))
)提前致谢。
这是我的代码片段,未使用自定义单元格的 NavigationLink。
@State var viewModel: UserListViewModel
@State private var selectedUserId: UUID?
init(viewModel: UserListViewModel) {
self.viewModel = viewModel
}
var body: some View {
NavigationStack {
ZStack {
List(viewModel.users ?? [], selection: $selectedUserId) {
CellView(user: $0)
}
.navigationDestination(item: $selectedUserId, destination: { key in
if let users = viewModel.users,
let index = users.firstIndex(where: { $0.id == key }) {
DetailView(user: users[index])
}
})
.navigationBarTitle(Text("Users"))
}
}
}
我也用过
.buttonStyle(PlainButtonStyle())
获取按钮操作(每个单元格上的 3 点按钮操作)以显示单元格菜单选项。