我希望我的表格列表有一个像iOS 8一样的可滑动菜单(最早是在iOS 7中引入的).

Screenshot of table view cell action buttons

我已经找到了a Ray Wenderlich guide that is clear个关于如何做到这一点,但它是一年零四个月前写的,代码是Objective-C.

iOS 8或即将推出的iOS 9最终是否在苹果的SDK中包含了该功能?我知道他们几年前就内置了"滑动显示删除功能".我不想浪费时间实现拼凑代码来模仿iOS 8邮件功能,如果苹果的新iOS将把它装在一个包装整齐的包里交给我的话.

推荐答案

试试这个,为Swift 3(Developer Docs)更新

override func tableView(_ tableView: UITableView, editActionsForRowAt: IndexPath) -> [UITableViewRowAction]? {
    let more = UITableViewRowAction(style: .normal, title: "More") { action, index in
        print("more button tapped")
    }
    more.backgroundColor = .lightGray
    
    let favorite = UITableViewRowAction(style: .normal, title: "Favorite") { action, index in
        print("favorite button tapped")
    }
    favorite.backgroundColor = .orange
    
    let share = UITableViewRowAction(style: .normal, title: "Share") { action, index in
        print("share button tapped")
    }
    share.backgroundColor = .blue
    
    return [share, favorite, more]
}

还要实现这一点:(你可以把它设置为有条件的,但这里的一切都是可编辑的)

override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

Swift相关问答推荐

数据不会被ARC删除在swift'

如何返回JSON数据?

OBJC代码中Swift 演员的伊瓦尔:原子还是非原子?

Swift中从头开始的基本HTTP客户端

使用变量(而非固定)的字符串分量进行Swift正则表达式

如何隐藏集合视图中特定的单元格,以避免出现空白空间?

当变量首次用于其自己的初始化时,如何清除变量...在初始化之前使用错误?

从 Swift Vapor Server 中调用 ShazamKit

状态变量更改后,SwiftUI 视图不会更改

如何在 SwiftUI 中将图像传递到 2 个视图

来自数据的 SwiftUI 图像

SwiftUI 4 列表初始化程序在 iOS 中不可用

为什么 `map(foo)` 和 `map{ foo($0) }` 返回不同的结果?

将项目引用添加到 Swift iOS XCode 项目并调试

在 Swift 中计算两个日期之间的差异

为什么我可以转换为 NSManagedObject 但不能转换为我的实体类型?

UICollectionView 自定义单元格在 Swift 中填充宽度

Swift 中惰性 var 的优势是什么

在 Swift 中为 UIPickerView 设置默认值?

如何在 Swift 中判断变量的类型