iOS 11 Table View Cell 滑動事件按鈕自定義

在 iOS 11 之前,我們使用:
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?
來對 Cell 的滑動手勢出現的按鈕進行自定義。
如下圖:

    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        
        let deleteAction = UITableViewRowAction(style: .destructive, title: "刪除") { (action, indexPath) in
            self.titles.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .left)
        }
        
        let archiveAction = UITableViewRowAction(style: .normal, title: "歸檔") { (action, indexPath) in
            
        }
        
        return [deleteAction, archiveAction]
    }

而在 iOS 11 之前,非常希望能實現系統那樣直接一滑到底刪除某個 Cell 的效果。
現在,從 iOS 11 開始提供了新的代理方法,并且在未來將要取代
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?
這個用了很久的方法。

新的方法提供了:左側按鈕自定義、右側按鈕自定義、自定義圖片、背景顏色,通過 UIContextualAction 來設置。

    // 左側按鈕自定義
    func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        
        let leftAction = UIContextualAction(style: .normal, title: "左側") { (action, view, finished) in
            
            finished(true)
        }
        
        
        return UISwipeActionsConfiguration(actions: [leftAction])
    }
    
    // 右側按鈕自定義
    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        
        let deleteAction = UIContextualAction(style: .destructive, title: "刪除") { (action, view, finished) in
            
            self.titles.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .automatic)

            // 回調告知執行成功,否則不會刪除此行!!!
            finished(true)
        }
        
        
        let archiveAction = UIContextualAction(style: .normal, title: "歸檔") { (action, view, finished) in
            
        }
        
        
        return UISwipeActionsConfiguration(actions: [deleteAction, archiveAction])
    }


Objective-C

- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UIContextualAction *deleteAction = [UIContextualAction
                                        contextualActionWithStyle:UIContextualActionStyleDestructive
                                        title:@"刪除"
                                        handler:^(UIContextualAction * _Nonnull action,
                                                  __kindof UIView * _Nonnull sourceView,
                                                  void (^ _Nonnull completionHandler)(BOOL))
    {
        [self.titles removeObjectAtIndex:indexPath.row];
        
        [self.tableView deleteRowsAtIndexPaths: [NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationAutomatic];
        
        completionHandler(true);
    }];
    
    // 設置按鈕圖片
    [deleteAction setImage:[UIImage imageNamed:@"Star"]];
    
    
    NSArray *actions = [[NSArray alloc] initWithObjects:deleteAction, nil];
    
    return [UISwipeActionsConfiguration configurationWithActions:actions];
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容