2012-04-14 2 views
1

UITableView를 코딩하는 방법을 배우려고하고 있으며 섹션 프로그래밍에 몇 가지 문제가 있습니다.중첩 된 NSArray의 UITableView에서 다른 섹션 헤더

나는 3 개의 배열로 문자열과 3 개의 배열로 3 개의 배열을 선언했다. 에

firstSection = [NSArray arrayWithObjects:@"Red", @"Blue", nil]; 
secondSection = [NSArray arrayWithObjects:@"Orange", @"Green", @"Purple", nil]; 
thirdSection = [NSArray arrayWithObject:@"Yellow"]; 

array = [[NSMutableArray alloc] initWithObjects:firstSection, secondSection, thirdSection, nil]; 

는 헤더

enter image description here

로 배열 자체 따라서 가능한 실제 이름을 사용 부분의 이름을 표시하는 표시 헤더

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ 

NSString * title; 
title = [NSString stringWithFormat:@"%@" , [array objectAtIndex:section]]; 

return title; 
} 

를 도시 firstSection 및 secondSection과 같은 배열의?

+0

나는 당신이 원하는 것을 정말로 이해하지 못합니다. - 배열의 이름입니다 (예 : "firstSection")? 또는 배열 안의 항목 (예 : "Red")? – SomaMan

+0

예 배열 이름을 표시하고 싶습니다. – wakaka

답변

5

처럼, 그것이 NSDictionary에 배열을 저장하는 것이 좋습니다 할 수 있습니다. 에 그런

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
    return [self.titleOfSections count]; 
} 
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section { 
    return [[self.tableContents objectForKey:[NSString stringWithFormat:@"%d",section]] count]; 
} 
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
    //Setting the name of your section 
    return [self.titleOfSections objectAtIndex:section]; 
} 

: 테이블 뷰 컨트롤러 방법에서 다음

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    //These will automatically be released. You won't be needing them anymore (You'll be accessing your data through the NSDictionary variable) 
    NSArray *firstSection = [NSArray arrayWithObjects:@"Red", @"Blue", nil]; 
    NSArray *secondSection = [NSArray arrayWithObjects:@"Orange", @"Green", @"Purple", nil]; 
    NSArray *thirdSection = [NSArray arrayWithObject:@"Yellow"]; 

    //These are the names that will appear in the section header 
    self.titleOfSections = [NSArray arrayWithObjects:@"Name of your first section",@"Name of your second section",@"Name of your third section", nil]; 

    NSDictionary *temporaryDictionary = [[NSDictionary alloc]initWithObjectsAndKeys:firstSection,@"0",secondSection,@"1",thirdSection,@"2",nil]; 
    self.tableContents = temporaryDictionary; 
    [temporaryDictionary release]; 
} 

예를 들어, 당신이 선언하고있는 경우이 같은 작업을 수행 할 수 있습니다, tableContents를라는 NSDictionary 변수 titleOfSections라는 NSArray을 합성 방법으로 각 배열의 내용에 액세스하십시오.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; 

    NSArray *arrayForCurrentSection = [self.tableContents objectForKey:[NSString stringWithFormat:@"%d",indexPath.section]]; 

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier]; 

    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] 
       initWithStyle:UITableViewCellStyleSubtitle 
       reuseIdentifier:SimpleTableIdentifier] autorelease]; 
    } 
    cell.textLabel.text = [arrayForCurrentSection objectAtIndex:indexPath.row]; 

    return cell; 
} 
+0

답장을 보내 주셔서 감사합니다. titleOfSections라는 세 번째 배열을 만들고 이름을 그 안에 넣었다는 것을 알았습니다. 그렇게하지 않고 배열 자체의 이름을 표시 할 수 있습니까? – wakaka

+0

@ wakaka 아니에요. – sooper

+0

글쎄, 나는 유일한 길이라고 생각한다. 감사합니다 – wakaka

0

유는 경우이

NSArray *SectionArray = [[NSArray alloc]initwithArray:[array objectAtIndex:section]]; 

    NSString * title; 

    title = [NSString stringWithFormat:@"%@" , [SectionArray objectAtIndex:section]]; 

    return title; 
+1

세 번째 섹션에는 항목이 하나뿐이므로 섹션 = 2 인 경우 인덱스가 범위를 벗어납니다. 나는 wakaka가 정말로 그가 원하는 것을 분명히했는지 확신하지 못한다 ... – SomaMan