iOS11 UITableViewCell滑动事件改动
生活随笔
收集整理的這篇文章主要介紹了
iOS11 UITableViewCell滑动事件改动
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在iOS8之后,蘋果官方增加了UITableView的右滑操作接口
optional func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? 復制代碼在這個方法中可以定義所需要的操作按鈕(刪除、置頂等),這些按鈕的類就是UITableViewRowAction。這個類定義按鈕的顯示文字、背景色和事件。并且返回數組的第一個元素在UITableViewCell的最右側顯示,最后一個元素在最左側顯示。
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {let deleteAction = UITableViewRowAction.init(style: .destructive, title: "Delete") { (action, indexpath) inself.dataArray.removeObject(at: indexpath.row)tableView.deleteRows(at: [indexpath], with: .left)}let markAction = UITableViewRowAction.init(style: .normal, title: "Mark") { (action, indexpath) in}return [deleteAction, markAction] } 復制代碼在iOS11中新增了兩個代理方法
- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath; - (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath; 復制代碼新的方法提供了:左側按鈕自定義、右側按鈕自定義、自定義圖片、背景顏色,通過 UIContextualAction 來設置
// 左側按鈕自定義 override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {let leftAction = UIContextualAction.init(style: .normal, title: "leftAction", handler: { (action, view, completionHandler) incompletionHandler(true)})let configuration = UISwipeActionsConfiguration.init(actions: [leftAction])return configuration } // 右側按鈕自定義 override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {// 刪除操作let deleteAction = UIContextualAction.init(style: .destructive, title: "delete", handler: { (action, view, completionHandler) incompletionHandler(true)})// 給按鈕設置背景圖片deleteAction.image = UIImage.init(named: "icon_del")let addAction = UIContextualAction.init(style: .normal, title: "add") { (action, view, completionHandler) in}// 可以修改按鈕的背景色addAction.backgroundColor = UIColor.purplelet configuration = UISwipeActionsConfiguration.init(actions: [deleteAction, addAction])return configuration } 復制代碼創建UIContextualAction對象時,UIContextualActionStyle有兩種類型,如果是置頂、已讀等按鈕就使用。UIContextualActionStyleNormal類型,delete操作按鈕可使用UIContextualActionStyleDestructive類型,當使用該類型時,如果是左滑操作,一直向左滑動某個cell,會直接執行刪除操作,不用再點擊刪除按鈕。
滑動操作還有一個需要注意的點,當cell高度較小時,會只顯示image,不顯示title,當cell高度夠大時,會同時顯示image和title。
總結
以上是生活随笔為你收集整理的iOS11 UITableViewCell滑动事件改动的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 神庙逃亡4
- 下一篇: js简单验证码的生成和验证