日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS11 UITableViewCell滑动事件改动

發布時間:2023/12/31 编程问答 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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滑动事件改动的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。