2011-03-04 3 views
0

나는 here 자습서를 따라 테이블을 그룹화 된 모양으로 만드는 방법을 궁금해하고있었습니다.그룹화 된 테이블보기 Obj-C

예 :

그룹 1은 서브 뷰 하나와 서브 뷰 두

그룹 2 내가 인터페이스 빌더에서 유형을 전환하지만 그건 하나 개의 그룹을 보여줍니다

서브 뷰 세 포함이 포함되어 있습니다.

덕분에, 아담

(!) 참고 *이 나는 따라서 튜토리얼, 목표 C에서 완전히 새로운 오전.

편집이 나는, 인터페이스 빌더에서 코드

#import "RootViewController.h" 
#import "SubViewOneController.h" 
#import "SubViewTwoController.h" 


@implementation RootViewController 


#pragma mark - 
#pragma mark View lifecycle 

-(void)awakeFromNib { 
    views = [[NSMutableArray alloc] init]; 

SubViewOneController *subViewOneController = [[SubViewOneController alloc] init]; 
SubViewTwoController *subViewTwoController = [[SubViewTwoController alloc] init]; 

//Subview 1 

    subViewOneController.title = @"Subview One"; 
    [views addObject:[NSDictionary dictionaryWithObjectsAndKeys: 
         @"Subview One",   @"title", 
         subViewOneController,  @"controller", 
         nil]]; 
    [subViewOneController release]; 

//Subview 2 

subViewOneController = [[SubViewOneController alloc] init]; 
subViewOneController.title = @"Subview Two"; 
[views addObject:[NSDictionary dictionaryWithObjectsAndKeys: 
        @"Subview Two",   @"title", 
        subViewTwoController,  @"controller", 
        nil]]; 
[subViewOneController release]; 

//Subview 3 

subViewOneController = [[SubViewOneController alloc] init]; 
subViewOneController.title = @"Subview Three"; 
[views addObject:[NSDictionary dictionaryWithObjectsAndKeys: 
        @"Subview Three",   @"title", 
        subViewOneController,  @"controller", 
        nil]]; 
[subViewOneController release]; 

UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init]; 
temporaryBarButtonItem.title = @"Back"; 
self.navigationItem.backBarButtonItem = temporaryBarButtonItem; 
[temporaryBarButtonItem release]; 

self.title = @"Basic Navigation"; 
} 

- (void)didReceiveMemoryWarning { 
[super didReceiveMemoryWarning]; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
return 2; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
return [views count]; 
} 
// 
//I think it goes somewhere in here 
// 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewStyleGrouped reuseIdentifier:CellIdentifier]; 
} 

cell.text = [[views objectAtIndex:indexPath.row] objectForKey:@"title"]; 


return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
UIViewController *targetViewController = [[views objectAtIndex:indexPath.row] objectForKey:@"controller"]; 
[[self navigationController] pushViewController:targetViewController animated:YES]; 
} 

- (void)dealloc { 
[views dealloc]; 
[super dealloc]; 
} 


@end 

답변

1

확인

경우 ([indexPath 섹션] == 0) 첫 번째 섹션에 대한 { 가 // 물건. }

if ([indexPath section] == 1) { // 두 번째 섹션의 내용. }

배열에서 셀을 구성하는 방법도 다루어야합니다. 섹션 행 번호는 0부터 시작합니다. didSelectRowAtIndexPath와 동일한 작업 : 셀을 설정하는 섹션을 처리하지 않는 경우.텍스트는

서브 뷰 하나

서브 뷰 두

서브 뷰 하나

나는 이미 인터페이스 빌더에서 설정 한 iPhone Programming (The Big Nerd Ranch Guide)

0

의 조각을 넣어 당신의 jQuery과 개체를 선택하고 스타일로 그룹화를 선택하는 것이 도움이 될 줄 알았는데.

프로그래밍 방식으로 만들 경우 initWithStyle:UITableViewStyleGrouped을 사용하십시오.

편집 :

if (section == 0) { /* your first section */ } 

if (section == 1) { /* your second section */ } 
+0

을 받고 추천으로 종료됩니다. 내 문제는 내가 각 그룹에 어떤 그룹을 추가 할 것인지 알려주는 방법을 모른다는 것입니다. –

+0

어디에서 그런 뜻이됩니까? 내 루트보기 컨트롤러에 넣어 있지만 "섹션"선언되지 않은 말한다. 내가 뭔가 분명하게 분실했는지 말해줘. –

+0

분명히 Apple 문서를 읽어야합니다. 내가 제공 한 코드는'cellForRowAtIndexPath' 델리게이트에 올 것입니다. – WrightsCS

1

이것은 모든 UITableViewDataSource에 의해 제어됩니다. numberOfSectionsInTableView: 메서드는 그룹 수를 제어하고 tableView:numberOfRowsInSection:은 각 그룹에있는 행 수를 제어합니다.

UITableViewDelegate의 tableView:cellForRowAtIndexPath:은 NSIndexPath 개체를 가져옵니다. indexPath.section은 어떤 그룹인지 알려주고 indexPath.row은 그룹의 어떤 행을 알려줍니다. 반환하는 셀은 실제로 어떤 그룹인지 또는 그룹의 어떤 행인지 전혀 알지 못합니다. tableView:cellForRowAtIndexPath:이 호출되면 특정 indexPath에 대해 반환한다는 사실에 의해 모두 제어됩니다. 당신이 그런 짓을 찾고됩니다 : 당신이 당신의 cellForRowAtIndexPath로 이동 않습니다 올바른 그래서