UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 40.0)];
[self.view addSubview:toolbar];
[toolbar release];
myTable = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
->
myTable = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 40.0, 320.0, 480.0 - 40.0 - 20.0) style:UITableViewStylePlain
* 참고
CGRect함수는 어떤 영역에 지정 크기만큼 공간을 할당해주는 역할을합니다.
CGRect cg = CGRectMake(10,10,100,100);//10,10좌표에서부터 100,100만큼 공간을 할당합니다.
- CGRectMake와 NSMakeRect의 차이 ..
CGRectMake가 결국은 NSMakeRect를 사용합니다. Cocoa는 맥에서 쓰이는 SDK인데, 이것 위에다 iPhone SDK를 만들었거든요. iPhone개발할 때는 CGRectMake를 쓰시면 됩니다.
CGRECT와 관련
CGRectMake( x, y, width, height ); : CGRect 생성
NSStringFromCGRect( CGRect ); : String으로 변환
CGRectFromString( String ); : String을 CGRect으로 변환
CGRectInset( CGRect, CGFloat, CGFloat ); : 같은 위치, 가운데 정렬, CGRect 생성
CGRectIntersectsRect( CGRect, CGRect ); : 충돌여부
CGRectZero : x, y를 0으로
(BOOL) CGRectContainsPoint(CGRect rect, CGPoint point); : //포인트가 사각형에 속했는지 판단합니다.
(BOOL) CGRectContainsRect(CGRect rect1, CGRect rect2); : //사각형1에 사각형2가 속해있는지 판단합니다.
(BOOL) CGRectIntersectsRect (CGRect rect1, CGRect rect2); : //사각형이 서로 교차했는지 판단합니다.
toolbar.items = [NSArray arrayWithObjects:
[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:nil] autorelease],
nil];
돌려 보겠습니다
[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(action)] autorelease],
- (void)action {
}
지워지는가 싶더니 지워져야할 셀이 그대로 있습니다
원인은 지워질때 호출되는 함수가 구현되어 있지 않아서 입니다
지워진다는 명령만 뜰뿐 실제로 셀을 지우는 코드가 없어서 입니다.
그냥 구현해 보기 전에
데이터와 테이블을 연동해 보겠습니다
0번(NSDictionary) {
그룹명(NSString) : "그룹 1"데이터(NSMutableArray) : {
0번 (셀, NSDictionary) {
텍스트 (NSString): "텍스트"
}
1번 (셀) {
}
2번 (셀) {
}
...
}
}
1번 {
그룹명 : "그룹 2"데이터 : {
0번 (셀) {
텍스트 : "텍스트"
}
1번 (셀) {
}
2번 (셀) {
}
...
}
}
NSMutableArray *myList;
myList = [[NSMutableArray alloc] init];
myList = [NSMutableArray array];
[[NSMutableArray alloc] init] [NSMutableArray array]
[[NSString alloc] initWithString:@""] [NSString string]
[[NSDictionary alloc] initWith...]; [NSDictionary dictionaryWithObject...
[myList addObject:
[NSDictionary dictionaryWithObjectsAndKeys:
@"그룹1", @"grouptitle",
[NSMutableArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:@"cell1",@"text", nil],[NSDictionary dictionaryWithObjectsAndKeys:@"cell2",@"text", nil],[NSDictionary dictionaryWithObjectsAndKeys:@"cell3",@"text", nil],[NSDictionary dictionaryWithObjectsAndKeys:@"cell4",@"text", nil],
nil],@"data",
nil]
];
[myList addObject:
[NSDictionary dictionaryWithObjectsAndKeys:
@"그룹2", @"grouptitle",
[NSMutableArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:@"dell1",@"text", nil],[NSDictionary dictionaryWithObjectsAndKeys:@"dell2",@"text", nil],[NSDictionary dictionaryWithObjectsAndKeys:@"dell3",@"text", nil],[NSDictionary dictionaryWithObjectsAndKeys:@"dell4",@"text", nil],
nil],@"data",
nil]
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
이걸
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [myList count];
}
수정합니다
지금은 간단한 샘플을 만드는거라 괜찬지만 복잡한 코딩을 하시게 되면 여기서 주의 하실것이 있습니다
myList의 count가 0이 될 경우도 생각해 둬야 합니다
0을 돌려주게 되면 어플이 튕기게 됩니다
저의 경우
return [myList count];
대신에
if (!myList) return 1;
return ([myList count] == 0 ? 1: [myList count]);
사용합니다
지금은 myList의 갯수를 건들일이 없으니 그냥 넘어 갑니다
이번에는 그룹 헤더의 문자를 돌려주는 함수를 수정해 보겠습니다
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [NSString stringWithFormat:@"test %i",section];
}
을
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [[myList objectAtIndex:section] objectForKey:@"grouptitle"];
}
로 수정
그리고 셀의 갯수 리턴 함수
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
return 7;
}
을
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
return [[[myList objectAtIndex:section] objectForKey:@"data"] count];
}
마지막으로 셀 정의 함수만 처리하면 끝이군요
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
switch문은 필요없으니 그만 지웁니다.
함수를 보시면 딸려 오는 인자에서 indexPath가 있습니다
여태껏 indexPath.row만 썼습니다만
indexPath는 그룹번호(섹션)와 셀번호(로우)를 같이 가지고 있는 변수입니다
불필요한 셀을 설정하는 코드를 지워주고 다음 코드를 넣습니다
cell.text = [[[[myList objectAtIndex:indexPath.section] objectForKey:@"data"] objectAtIndex:indexPath.row] objectForKey:@"text"];
좀 알아보기 어렵나요? 이건 어떻나요?
NSDictionary *group = [myList objectAtIndex:indexPath.section];
NSMutableArray *cells = [group objectForKey:@"data"];
NSDictionary *oneCell = [cells objectAtIndex:indexPath.row];
cell.text = [oneCell objectForKey:@"text"];
한번 돌려 보겠습니다
잘뜹니다. 이제부터 본격 편집을 구현 해보겠습니다.
다시한번 UITableView의 헤더를 보면 Editing에 대한 델리게이트 및 데이터소스가 있습니다
우선 중요한 것만 써보죠
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
우선 위 함수는 테이블이 편집 모드에 들어갈때 호출됩니다
셀 하나당 한번씩 호출되며 헤당 셀이 편집될수 있는지 물어보는겁니다
YES를 돌려주면 헤당셀이 편집 가능하다는 거고 NO를 돌려주면 편집이 안됩니다
위 함수를 구현 하지 않으면 자동적으로 모든셀에 YES가 들어갑니다
잠시 가지고 놀아보겠습니다
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return (indexPath.row % 2 == 0);
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
NSDictionary *source = [NSDictionary dictionaryWithDictionary:[[[myList objectAtIndex:sourceIndexPath.section] objectForKey:@"data"] objectAtIndex:sourceIndexPath.row]];
[[[myList objectAtIndex:sourceIndexPath.section] objectForKey:@"data"] removeObjectAtIndex:sourceIndexPath.row];
[[[myList objectAtIndex:destinationIndexPath.section] objectForKey:@"data"] insertObject:source atIndex:destinationIndexPath.row];
}
여기까지 데이터와 테이블 편집을 구현해봤습니다.
'개발 > App Developer' 카테고리의 다른 글
IB 없이 하기, UITableView끝장보기 (2) 그룹과 셀 (0) | 2010.09.06 |
---|---|
IB 없이 하기, UITableView끝장보기 (3) 셀의 구조 (1) | 2010.09.06 |
Foundation Kit (0) | 2010.09.06 |
메소드와 메세지 (0) | 2010.09.06 |
HIG 번역하다 말았네요 (0) | 2010.09.06 |