[iOS 개발] 테이블 생성 및 글자 넣어 출력해보기
TableControlViewController.h 파일 소스 (사용할 배열 정의)
──────────────────────
#import <UIKit/UIKit.h>
@interface TableControlViewController : UIViewController {
NSArray *listData;
}
@property (nonatomic, retain) NSArray *listData;
@end
──────────────────────TableControlViewController.m 파일 소스
──────────────────────
#import "TableControlViewController.h"
@implementation TableControlViewController
@synthesize listData;
/*
중간에 주석처리된 메소드들은 생략
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
// viewDidLoad는 뷰가 모두 로드되었을때 실행되는 메소드
- (void)viewDidLoad {
listData = [NSArray arrayWithObjects:@"권태성", @"KWON TAE SEONG", @"tskwon.tistory.com", @"facebook.com/kwontaeseong", nil];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [listData count] ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//indexPath를 이용해 각 테이블에 접근 할 수 있음
static NSString *cIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cIdentifier];
//dequeueReusableCellWithIdentifier 테이블 로우가 많아 내려갈때 갱신될때 새로 만들어진 행이 이전과 같을때는
//굳이 다시 보여줄 필요가 없으니 재사용 하기위해 정의할때 사용하는 메소드
if(cell == nil)
{
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cIdentifier]autorelease];
}
cell.textLabel.text = [listData objectAtIndex:indexPath.row];
return cell;
}
- (void)dealloc {
[listData release];
[super dealloc];
}
@end
컴파일 후 시뮬레이터를 실행하면 이런 모양의 테이블이 나옵니다.