[IPhone 09.01]04. TableView

2010. 9. 1. 00:44IPhone/Basic

  테이블뷰란?? 가장 아이폰에서 많이 사용되는 예중 하나로써 데이터가 리스트 형태로 정렬 되어 있는것을 말하는데 일반적으로 전화번호부 메시지관리등으로 관리 되며 제약 조건으로는 1개의 컬럼 그러니까....한개의 줄로만 이루어져있으며 한개의 줄의 보기위해 상하 스크롤만 가능하게 되어있다.
  테이블뷰는 크게 두 종류로 나뉘며 첫번째는 연속해서 목록이 나열되는 기본형태이며 두번째로는 항목들이 묶여서 다시 소분류로 나뉘어지는 그룹형태가 있다.

1. 메뉴판 프로그램
  가장 기본이 되는 프로그램을 예로 작성해볼때 먼저 Navigation Based Application이라는 템플릿을 선택하여 프로젝트를 생성한다. 인터페이스 빌드를 열어서 TableView를 긁어서 현재 View에 놓는다. 그럼 일단 뷰의 기본 구성은 마친 상태이며 바로 실행해보면 예로 어떻게 나타나는지 구성되어 있을 것이다.
다음 소스코드를 작성하면 일반적으로 메뉴판은 작성될것 이다.

처음에는 인터페이스 부분을 작성해주는데 인터페이스는 클래스를 의미하면 <>는 델리게이트를 의미한다.
클래스의 내부 변수를 사용하기 위해 프로토타입을 선언해준다.

@interface MyNaviAppDelegate:NSObject<UIApplicationDelegate>{
              IBOutlet UIWindow *window;
              IBOutlet UINavigationController *navigationController;
              NSMutableArray *list;
}
@property(nonatomic,retain)UIWindow *window;
@property(nonatomic,retain)UINavigationController *navigationController;

-(void)createDemoData;
@property(nonatomic,copy)NSArray *list;
-(NSUInteger)countOfList;
-(id)objectInListAtIndex:(NSUInteger)theIndex;
@end

위에서 선언만 되었던 메소드나 변수를 이제 실제로 사용하기 위해 implemention안에 넣는다.
웨이서 프로토타입을 선언해준것을 아래에 선언해준다.

 @sysnthesize list;
-(void)createDemoData{
             NSArray *strList;
             strList=[[NSArray alloc] initWithObject:@"육개장",@"탕수육",nil];
             self.list=strList;
             [strList release];
}
-(void)viewDidLoad{
            [super viewDidLoad];
            self.title=@"메뉴판";
}
-(NSInteger)numberOfSectionsInTableView:(UITableView*)talbeView{
            return 1;
}
-(NSInteger)tableView:(UITableView*)tableView numberOfRowInSection:(NSInteger)section){
            MyNaviAppDelegate *appController=(MyNaviAppDelegate*)[[UIApplication sharedApplication]delegate];
            [appController createDemoData];
            return [appController countOfList];
}
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
               MyNaviAppDelegate *appController=(MyNaviAppDelegate*)[[UIApplication sharedApplication]delegate];
               [appController createDemoData];
               return [appControll countOflist];
-(void)setList:(NSArray*)newList{
             if(list!=newList)
             {
                         [list release];
                         list=[newList copy];
             }
}
-(NSUInteger)countOfList{
           //목록의 갯수를 반환한다.
            return [list count];
}
-(UITableViewCell*)tableView:(UITableView *)tableview cellForAtIndexPath:(NSIndexPath*)index path
{
          static NSString *MyIdentifier;
          UITableViewCell *cell=[tableViewCell alloc]UITableViewStylePlain
}
-(id)objectInListAtIndex:(NSUInteger)theIndex{
            return [list objectAtIndex:theIndex];
}

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
            static NSString *MyIdentifier=@"MyIdentifier";
            UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
            if(cell==nil){
                   cell=[[[UITableViewCell alloc]initWithStyle:UITableViewStylePlainreuseldentifiere:MyIdentifier]autorelease];
            }
            MyNaviAppDelegate *appController=(MyNaviAppDelegate*)[[UIApplication sharedApplication]delegate];
            cell.textLabel.text=[appController objectInListAtIndex:indexPath.row];
            return cell;     
}

터치하면 체크 표시가 토글되도록 되어 있다.
 -(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
           UITableViewCellAccessoryType theCheckMark;
           theCheckMark=[tableView cellForRowAtIndexPath:indexPath].accessoryType;
           if(theCheckMark==UITableViewCellAccessoryNone)
                   theCheckMark=UITableViewCellAccessoryCheckmark;
           else
                   theCheckMark=UITableViewCellAccessoryNone;
           [tableView cellForRowAtIndexPath:indexPath].accessoryType=theCheckMark;
}

이외에 마크부분에 UITableViewCellAccessory 뒤에 Checkmark이외에도 Disclosure , Detail를 사용할수 있다.