1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| import SwiftUI struct Message: Identifiable { var id = UUID() var title: String var last: String } // 定义数组,存放数据 var Messages = [ Message(title: "充当英翻中", last: "很好,让我们开始为想要减肥的人设计一个锻炼计划。首先,我们需要了解他们的目标和当前的健身水平。您可以问他们关于以下问题的信息:您的身高、体重和BMI(身体质量指数)是多少?您每周进行多少次有氧运动和力量训练?您每次进行运动的时间是多长?您的饮食习惯是什么?您是否有任何特殊的饮食限制?您的工作和日常活动水平是什么?一旦您了解了他"), Message(title: "担任私人教练", last: ""), Message(title: "随便聊聊", last: ""), Message(title: "随便聊聊", last: ""), Message(title: "随便聊聊", last: "好的,这是一个小学生的笑话:"), ] struct DialogueView: View { @State var messagesItems = Messages @State var showActionSheet = false var body: some View { NavigationView{ List { ForEach(messagesItems) { Message in HStack { Image("dialogue") .resizable() .renderingMode(.template) .foregroundColor(.gray) .frame(width: 40, height: 40) VStack { Text(Message.title) .foregroundColor(.black) .listRowSeparator(.hidden) .font(.system(size: 17).weight(.light)) .frame(maxWidth: .infinity, alignment: .leading) .padding(1) Text(Message.last) .foregroundColor(.gray) .frame(maxWidth: .infinity, alignment: .leading) .font(.system(size: 15)) .lineLimit(1) .padding(1) } Image("drag") .resizable() .frame(width: 20, height: 20) }.contextMenu { Button(action: { // 点击删除 // self.delete(item: Message) // 点击打开ActionSheet弹窗 self.showActionSheet.toggle() }) { HStack { Text("删除") Image(systemName: "trash") } } } // ActionSheet弹窗 .actionSheet(isPresented: self.$showActionSheet) { ActionSheet( title: Text("你确定要删除此项吗?"), message: nil, buttons: [ .destructive(Text("删除"), action: { //点击删除 self.delete(item: Message) }), .cancel(Text("取消")) ]) } } .onDelete(perform: deleteRow) .onMove(perform: moveItem) } .navigationBarItems(leading: HStack { Image(systemName: "trash") .resizable() .frame(width: 20, height: 20) .foregroundColor(.gray) },trailing: HStack { Button("新对话") { print("Specials tapped!") } } ) } } // 滑动删除方法 func deleteRow(at offsets: IndexSet) { messagesItems.remove(atOffsets: offsets) } // 拖动排序方法 func moveItem(from source: IndexSet, to destination: Int) { messagesItems.move(fromOffsets: source, toOffset: destination) } //contextMenu 删除的方法 func delete(item Message: Message) { if let index = self.messagesItems.firstIndex(where: { $0.id == Message.id }) { self.messagesItems.remove(at: index) } } }
struct DialogueView_Previews: PreviewProvider { static var previews: some View { DialogueView() } }
|