카테고리 없음

XX캠퍼스 강의 - ch07 _ UITableView 삭제, 순서변경

Machine_웅 2022. 6. 13. 17:14
728x90
반응형

삭제

    
class ViewController: UIViewController {
    @IBOutlet var editBtn: UIBarButtonItem!
    var doneBtn : UIBarButtonItem?
    
   
    override func viewDidLoad() {
        super.viewDidLoad()
        // edit 버튼
        self.doneBtn = UIBarButtonItem(barButtonSystemItem: .done, target: self,
        action:  #selector(doneBtnTap))
        ...
    }
    
        // object C 와 호환하기위해
    @objc func doneBtnTap(){
        self.navigationItem.leftBarButtonItem = self.editBtn
        self.tableView.setEditing(false, animated: true)
        
    }
    
    @IBAction func tabEditBtn(_ sender: UIBarButtonItem) {
        guard !self.tasks.isEmpty else {return}
        
        self.navigationItem.leftBarButtonItem = self.doneBtn
        self.tableView.setEditing(true, animated: true)
        
    }
    
   }
   
   extension ViewController: UITableViewDataSource {
   		...
        // 삭제버튼이 눌린 셀이 어떤것인지
    func tableView(_ tableView: UITableView, 
    	commit editingStyle: UITableViewCell.EditingStyle, 
    	forRowAt indexPath: IndexPath) {
        	self.tasks.remove(at: indexPath.row)
        	tableView.deleteRows(at: [indexPath], with: .automatic)
        
        	// 모든 행이 삭제됬을경우 돌아옥
        	if self.tasks.isEmpty{
           		self.doneBtnTap()
        	}
    }
   
   }

 

순서변경

// android의 adapter 느낌 ?
extension ViewController: UITableViewDataSource {
   ....
    
    // 셀이동
    func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    // 원래 위치랑 이동한 곳을 알려줌
    func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
            
    }
    
}

 

728x90
반응형