[iOS] 테이블뷰에서 아이템 삭제시 유의사항

RudinP·2025년 5월 19일
0

Study

목록 보기
268/273
  • 테이블뷰에서 아이템을 삭제할 경우 반드시 데이터를 삭제 한 뒤, 테이블뷰 아이템 삭제 순서로 진행해야한다.

1. 데이터 업데이트
2. 테이블뷰 업데이트

예시

override func tableView(_ tableView: UITalbeView, commit editingSytle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
	if editingStyle == .delete {
    	let target = contents[indexPath.row]
        
        if removeItem(at: target.url) {
        	//순서 바뀌면 안됨
        	contents.remove(at: indexPath.row)
            //해당 메소드 호출 시 데이터 수 일치하지 않으면 크래시 발생
        	tableView.deleteRows(at: [indexPath], with: .automatic)    
        }
    }
}
profile
iOS 개발자가 되기 위한 스터디룸...

0개의 댓글