본문 바로가기

General/iOS

[iPhone] Table View


아이폰 프로그래밍의 처음으로 테이블 뷰 만들기 부터 시작해보기로 했습니다.
Java 기반 개발과 너무나도 다른 현실.
Object-C는 ... 이게 무슨 소리인가 싶을정도였습니다.

어찌되었든. 아이폰 앱 개발. 이제 시작이네요.


테이블 뷰
- 데이터 셋해주기, 타이틀 셋하기
- 데이터 들어있는 Array release해주기 (Java와 다른 모습. 가비지컬렉션같은건 없습니다. 모두 직접해줍니다.)
- 테이블 뷰의 row 갯수 셋해주기 (이건 위의 데이터가 들어있는 array의 크기만큼으로 해줍니다.)
- Cell 셋해주기(cell initialize와 동시에 데이터(리스트에서 하나의 데이터)를 넣어주고 cell을 리턴해 줍니다.)



프로젝트 만들기
프로젝트는 네비게이션 기반 어플리케이션으로 생성합니다.

RootViewController.h 파일에 코드 추가
@interface RootViewController : UITableViewController 

{

NSMutableArray *listOfItems;

}







RootViewController.m 파일에 코드 추가

- 주석으로 되어있던 viewDidLoad 메소드를 주석해제하고 구현해 줍니다.

- (void)viewDidLoad {

    [super viewDidLoad];


listOfItems = [[NSMutableArray alloc] init];

// Initalize

[listOfItems addObject:@"유딩이유"];

[listOfItems addObject:@"초딩이유"];

[listOfItems addObject:@"중딩이유"];

[listOfItems addObject:@"고딩이유"];

[listOfItems addObject:@"아이유><"];

// set title

self.navigationItem.title = @"IU Love";

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.

    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

}  



- 크기가 0으로 되어있던 테이블 뷰의 row갯수를 list의 크기로 바꾸어 줍니다.

// Customize the number of rows in the table view.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [listOfItems count];

}     



- Cell을 초기화 하고 Cell에 위에서 만든 리스트의 데이터를 넣어줍니다.

// Customize the appearance of table view cells.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    

    static NSString *CellIdentifier = @"Cell";

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc

 initWithStyle:UITableViewCellStyleDefault 

 reuseIdentifier:CellIdentifier] autorelease];

    }

    

// Configure the cell.

NSString *cellValue = [listOfItems objectAtIndex:indexPath.row];

[[cell textLabelsetText:cellValue];

    return cell;

}  






이런 과정들을 마치고 나면 이런 결과가 나옵니다.
작은 첫발을 내딛는 순간이군요. iOS는 캡쳐하는것도 정말 편하네요. 또 한번 놀랍니다.
왠만한 필요한 기능들은 전부 구현이 되어있는듯하네요. 

 1. RootViewController.h 파일에 코드 추가
    - 데이터를 담을  Array 객체 선언

 2. RootViewController.m 파일에 코드 추가
    - 데이터 넣기,
    - 데이터를 담았던 객체 메모리 해제
    - 리스트 크기
    - Cell set
  







Table view
- 1개의 컬럼으로 구성
- 수직 스크롤 가능

클래스 이름
UITableView ( UIScrollView에서 상/하 스크롤 능력을 상속받아 사용 )

Table이라는 View사용을 위해  Model과 Controller를 추가해서 M-V-C 디자인 패턴을 완성
// data source(Model)와 delegate(Controller)

NSIndexPath

UITableViewCell