Project 04 - TableView

DaY·2021년 3월 25일
1

iOS

목록 보기
16/52
post-thumbnail

TableView를 옆으로 밀어 원하는 작업을 할 수 있다.

canEditRowAt indexPath로 tableView의 열을 편집 가능하게 한다.

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

editingStyleForRowAt indexPath로 밀었을 때 사용할 기능을 정한다.
editingStyle은 deleteinsert 가 있다.

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
    return .delete
}

각 Editing Style에 맞추어 실행할 작업을 지정해준다.

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        let target = DataManager.shared.todoList[indexPath.row]
        DataManager.shared.deleteTodo(target)
        DataManager.shared.todoList.remove(at: indexPath.row)
            
        todoListTableView.deleteRows(at: [indexPath], with: .fade)
    }
}

editActionsForRowAt indexPath을 사용하면 delete, insert 외에 원하는 액션을 custom해서 사용할 수 있다.

public override func tableView(_ tableVew: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {

    let 액션 명 = UITableViewRowAction(style: .default, title: "타이틀 명") { (action, indexPath) in     
        원하는 액션
    }
    액션 명.backgroundColor = UIColor.systemGray
    return [액션 명]
}

0개의 댓글