ios - 如何将滑动操作添加到列表?- 斯威夫特用户界面
问题描述
我想将滑动操作添加到我的列表中以显示一些操作。
我想要的只是在列表行上添加一些向左滑动手势的操作,例如 ios 的邮件应用程序。
我找到了这个@michał-ziobro Swipe Actions Modifier解决方案,但它不起作用:/
该代码的某些部分给了我错误,我用一些扩展修复了它们,我想这可能是它不起作用的原因。
struct TableViewConfigurator: UIViewControllerRepresentable {
var configure: (UITableView) -> Void = { _ in }
func makeUIViewController(context: UIViewControllerRepresentableContext<TableViewConfigurator>) -> UIViewController {
UIViewController()
}
func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<TableViewConfigurator>) {
let tableViews = UIApplication.nonModalTopViewController()?.navigationController?.topViewController?.view.allSubViewsOf(type: UITableView.self) ?? [UITableView]()
for tableView in tableViews {
self.configure(tableView)
}
}
}
struct ListSwipeActions: ViewModifier {
@ObservedObject var coordinator = Coordinator()
func body(content: Content) -> some View {
return content
.background(TableViewConfigurator(configure: { tableView in
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
tableView.delegate = self.coordinator
}
}))
}
class Coordinator: NSObject, ObservableObject, UITableViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
print("Scrolling ....!!!")
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return .delete
}
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let isArchived = false
let title = isArchived ? "Unarchive" : "Archive"
let archiveAction = UIContextualAction(style: .normal, title: title, handler: {
(action, view, completionHandler) in
// update data source
completionHandler(true)
})
archiveAction.title = title
archiveAction.image = UIImage(systemName: "archivebox")!
archiveAction.backgroundColor = .systemYellow
let configuration = UISwipeActionsConfiguration(actions: [archiveAction])
return configuration
}
}
}
extension List {
func swipeActions() -> some View {
return self.modifier(ListSwipeActions())
}
}
这是我的列表视图:
struct NotificationsTab: View {
@ObservedObject var viewModel = NotificationsVM()
func delete(at offsets: IndexSet) {
viewModel.notifications.remove(atOffsets: offsets)
}
var body: some View {
NavigationView {
Group{
if viewModel.notifications.count > 0 {
List{
ForEach(self.viewModel.notifications, id: \.id){ notification in
HStack(spacing: 15){
WebImage(url: URL(string: notification.sender!.avatar!.userAvatarPathSmall()))
.resizable()
.indicator(.activity)
.aspectRatio(contentMode: .fill)
.animation(.easeInOut(duration: 0.5))
.transition(.fade)
.frame(width: 48, height: 48)
.cornerRadius(24)
Text(notification.sender!.name!).fontWeight(.bold) + Text(notification.message!)
}.padding(.vertical)
}.onDelete(perform: delete)
}.swipeActions()
}else{
LoadingView()
}
}
.navigationBarTitle("Notifications")
}.onAppear(){
self.viewModel.getNotifications()
}
}
}
struct NotificationsTab_Previews: PreviewProvider {
static var previews: some View {
NotificationsTab()
}
}
解决方案
推荐阅读
- javascript - Javascript:如何从 utf-8 编码到 iso-8859-1 并从中解码
- python - 在 Flask 中将图像保存为错误文件(0 字节)
- java - 这与 Firestore 读取请求有关。在 Android 应用程序中实现的 Firestore
- javascript - 简单的 HTML/JS 碰撞检测不起作用
- python - 如何使用特定颜色为矩阵着色?
- c - Memset struct 变量分别与 memset 整个结构,哪个更快?
- sql - 加入 2 个表一个值
- reactjs - 如果索引页面,则从路径中删除 #/
- python - 使用 Map 和 Filter 函数查找地址 - Python
- python - 如何更改 django admin 更改列表页面选项卡的标题