3.0으로 업그레이드 되면서 테이블에서 UITableViewCell의 생성 코드가 바뀌었다.
컴파일을 어느 버젼에 맞추느냐에 따라 오류가 나는 코드와 안나는 코드가 있으니...어지럽다.
역시나 나쁜 머리를 위해 이곳에 변경된 내용만 기록을 한다.
기존 UITableViewCell을 생성하는 코드는 보통 아래와 같다. 보통 "
initWithFrame"을 사용한다.
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell-page";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
if(currentPage == ([indexPath row]))
cell.text = [NSString stringWithFormat:@"%d 장 - 독서중", [indexPath row]+1];
else
cell.text = [NSString stringWithFormat:@"%d 장", [indexPath row]+1];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
그러나 3.0에 오면서 더이상 "initWithFrame"을 사용하지 않고 "
initWithStyle"을 사용하도록 바뀌었다. 아래는 애플의 UITableView관련 문서에 나와 있는 예제 코드이다.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSDictionary *item = (NSDictionary *)[self.content objectAtIndex:indexPath.row];
cell.textLabel.text = [item objectForKey:@"mainTitleKey"];
cell.detailTextLabel.text = [item objectForKey:@"secondaryTitleKey"];
NSString *path = [[NSBundle mainBundle] pathForResource:[item objectForKey:@"imageKey"] ofType:@"png"];
UIImage *theImage = [UIImage imageWithContentsOfFile:path];
cell.imageView.image = theImage;
return cell;
}
생성 스타일 값은 아래와 같다.
typedef enum {
UITableViewCellStyleDefault,
UITableViewCellStyleValue1,
UITableViewCellStyleValue2,
UITableViewCellStyleSubtitle
} UITableViewCellStyle;
현재 개발 테스트를 2.xx에 맞춰서 하고 있어서 바뀐 코드를 적용하면 100% 오류가 나고 있다. -.-;;
많은 개발자들이 하위 호환성에 대해서 테스트 해 봤을테니...나도 3.0으로 올라 타 봐야 겠다.
관련 참고 내용은 아래를 참고, pdf로도 저장하여 볼 수 있다.
http://developer.apple.com/iphone/library/documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html