2012-04-14 17 views
1

UITableViewのコード方法を学習しようとしていて、セクションのプログラミングにいくつかの問題があります。ネストされたNSArrayのUITableViewの異なるセクションヘッダー

私は3つの配列で文字列と1つの配列を持つ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という名前の3番目の配列を作成し、そこに名前を入れていることがわかります。そうすることなく配列自体の名前を表示することは可能ですか? – wakaka

+0

@ wakaka私が知っているわけではありません。 – sooper

+0

私はそれが唯一の方法だと思います。ありがとう – wakaka

0

uはあなたの場合には、この

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

    NSString * title; 

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

    return title; 
+1

これはthirdSectionが1つのアイテムしか持たないので動作しません。したがって、セクション= 2の場合、インデックスは範囲外です。 wakakaが本当に自分が望んでいることを本当に明確にしているかどうかはわかりません。 – SomaMan

関連する問題