IT분야/iOS
[iOS] UITableView 행 삭제
suroMind
2011. 7. 14. 14:59
간단한 UITableView의 삭제기능이다.
1. 네비게이션바에 Edit 버튼을 추가해준다.
- (void) viewDidLod{
......................
......................
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(delList:)] autorelease];
}
2. 버튼을 눌렀을 때 호출될 메소드를 만들어 준다. 버튼을 누르면 테이블뷰에 마이너스 버튼이 생긴다.
- (IBAction)delList:(id)sender{
BOOL editing = !(self.editing);
[self setEditing:editing animated:YES];
}
3. 삭제를 했을 시 수행하게 될 작업을 코딩한다.
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
ArticleModel *article = [articles objectAtIndex:indexPath.row];
[[HTTPManager getSqlDao] tpsDelete:article.tpsNum]; // 디비에서 삭제
// (HTTPManager와 ArticleModel은 본인이 작성한 클래스 이므로 알아서 변경하시면 되겠지요)
[self.articles removeObjectAtIndex:indexPath.row]; //NSArray의 해당되는 데이터 삭제
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; //테이블뷰에서 삭제
}
}
}
}