테이블뷰 사용시 cell을 동적으로 추가하거나 삭제해야 하는 경우가 발생한다.
이럴 때 사용하는 소스코드
예를들어
cuscell *cus; //CustomCell UITableView *table; NSMutableArray *data; data = [NSMutableArray arrayWithObjects:@"Item 1", @"Item 2", nil]; |
를 선언하고 테이블뷰에서 data의 값을 가져온다고 하자.
cell 추가
[self.data addObject:@"New Item"];
[table beginUpdates];
NSArray *paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:1 inSection:0]];
[table insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationRight];
[table endUpdates];
cell 삭제
//커스텀 셀(cuscell)에 선언된 버튼을 눌렀을 때 셀을 삭제하는 형식
IndexPath *indexPath = [table indexPathForCell:(cuscell *)[[sender superview] superview]];
[data removeObjectAtIndex:indexPath.row];
[table beginUpdates];
[table deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
[table endUpdates];
# withRowAnimation에 다양한 애니메이션을 삽입할 수 있다.
'IT > iOS' 카테고리의 다른 글
iOS5 Automatic reference counting (0) | 2012.06.19 |
---|---|
Custom Actionsheet (커스텀 액션쉬트 이미지 넣기) (0) | 2012.06.14 |
앱에서 다른 앱 호출 (0) | 2012.06.13 |
View Animation (뷰 애니메이션) (0) | 2012.06.12 |
Couldn't register com.yourcompany with the bootstrap server. Error: unknown error code. (0) | 2012.05.24 |
UITextView 줄바꿈 (0) | 2012.05.23 |
MAC에서 특수문자(Special Characters) 입력 (0) | 2012.05.23 |
UITableview 기본 메소드 (0) | 2012.05.22 |