UITableView
Configuring a Table View
– tableView:cellForRowAtIndexPath:
required method– numberOfSectionsInTableView:
– tableView:numberOfRowsInSection:
required method– sectionIndexTitlesForTableView:
– tableView:sectionForSectionIndexTitle:atIndex:
– tableView:titleForHeaderInSection:
– tableView:titleForFooterInSection:
Inserting or Deleting Table Rows
Reordering Table Rows
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Parameters
- tableView
A table-view object requesting the cell.
- indexPath
An index path locating a row in tableView.
Return Value
An object inheriting from
UITableViewCell
that the table view can use for the specified row. An assertion is raised if you returnnil
.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
Parameters- tableView
The table-view object requesting this information.
- section
An index number identifying a section in tableView.
Return Value
The number of rows in
section
.
시작>
인터페이스 빌더에서 원하는 View에 테이블뷰를 올려 놓으면, 처리하고자 하는 콘트롤러에 Outlet을 DataSource와 Delegate를 연결 시켜주어야 한다.
예제>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5 ; //테이블의 길이 5줄
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cIdentifier =@"Cell"; //TableView에 들어갈 NSString 포인터 정의
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cIdentifier];
//UITableViewCell에 들어갈 객체의 정의 / 이미 나온 셀이면 재사용 할 식별자=Cell
if(cell ==nil) //cell객체가 초기화가 안되어 있으면..
{
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cIdentifier]autorelease];
//기본 스타일로 정의하고 셀 한개한개 릴리즈 할 수 없으니 오토릴리즈..
}
if (indexPath.row ==0) cell.textLabel.text = @"첫번째 셀입니다."; //0~4중 첫번째 셀이면 해당 텍스트를 넣어준다.else cell.textLabel.text = @"나머지 셀입니다."; // 나머지는 모두 해당 텍스트를 넣어준다.
return cell;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
printf("%d\n",indexPath.row); //테이블 뷰의 셀이 선택되어졌을때 호출되었을때 리턴값을 출력한다.
//네이게이션 콘트롤러등에서 테이블 뷰가 호출 되었을떄 해당 콘트롤러를 호출할떄 응용한다.
}
응용>
1. 헤더에 NSArray *listData; 정의해주고 property와 synthesize를 해준다.
2. view처리가 끝났을때 배열에 셀 문자 정의
- (void)viewDidLoad {
listData = [NSArray arrayWithObjects:@"첫번째 셀", @"두번째 셀", @"세번째 셀",@"네번째 셀",@"다섯번째셀",nil];
[super viewDidLoad];
}
3. 오버라이딩 메소드 처리
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [listData count]; //배열의 카운트 만큼 셀을 잡아준다.
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cIdentifier];
if(cell ==nil)
{
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cIdentifier]autorelease];
}
cell.textLabel.text = [listData objectAtIndex:indexPath.row]; //배열의 순서와 맞추어 cell에 넣어준다.
return cell;
}
4. 배열 잡아준것 메모리 릴리즈
- (void)dealloc {
[listData release];
[super dealloc];
}
응용2>
웹에서 xml을 파싱하여 셀에 넣고 처리한다.
-- UITableViewCellStyle 정리 --
UITableViewCellStyleDefault
UITableViewCellStyleValue1
UITableViewCellStyleValue2
UITableViewCellStyleSubtitle
'Xcode > Examples' 카테고리의 다른 글
xcode 4.2 Navigation Controller exam (0) | 2011.10.13 |
---|---|
iPhone에서 XML파일 파싱하기 (0) | 2011.09.23 |