-3

重複の可能性:
iPhone UITableView セクション

基本的に、2 つのテーブルを 2 つの異なるセクションに分けようとしている人がいますが、何を試してもうまくいかないようです。私のコードがあります。どこが間違っているのか教えてください。例を教えていただければ幸いです。誰もこれを解決できず、私はしばらくの間本当に行き詰まりました。ありがとうサム・ホートン。

#import <Foundation/Foundation.h>


@interface FirstLevelViewController : UITableViewController {
    NSArray *controllers;
    NSMutableArray *male;
    NSMutableArray *female;
}

@property (nonatomic, retain) NSArray *controllers;
@property (nonatomic, retain) NSMutableArray *male;
@property (nonatomic, retain) NSMutableArray *female;

@end



#import "FirstLevelViewController.h"
#import "SecondLevelViewController.h"
#import "DisclosureDetailController.h"
#import "SecondVoiceController.h"
#import "ThirdVoiceController.h"

@implementation FirstLevelViewController
@synthesize controllers,male,female;

-(void)viewDidLoad {
    self.title = @"Voices";
    DisclosureDetailController *th = [DisclosureDetailController alloc];
    th.title = @"First Voice";
    [male addObject:th];
    [th release];

    SecondVoiceController *array2 = [SecondVoiceController alloc];
    array2.title = @"Second Voice";
    [male addObject:array2];
    [array2 release];

    ThirdVoiceController *array3 = [ThirdVoiceController alloc];
    array3.title = @"Third Voice";
    [female addObject:array3];
    [array3 release];

    self.controllers = male;
    [female release];
    [male release];
    [super viewDidLoad];
}

-(void)viewDidUnload {
    self.male = nil;
    self.female = nil;
    self.controllers = nil;
    [super viewDidUnload];
}

-(void)dealloc {
    [male release];
    [female release];
    [controllers release];
    [super dealloc];
}

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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
         switch(section){
                case 0:
                return [male count];
                break;
                case 1:
                return [female count];
                break;
             }
    }

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

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FirstLevelCell];
     if (cell == nil) {
         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FirstLevelCell] autorelease];
     }
     SecondLevelViewController *controller;
     switch([indexPath section]){
         case 0:
              controller = [male objectAtIndex: [indexPath row] ];
              break;
         case 1:
              controller = [female objectAtIndex: [indexPath row] ];
              break;
     }
     cell.textLabel.text = controller.title;
     cell.imageView.image = controller.rowImage;
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
     return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger row = [indexPath row];
    SecondLevelViewController *nextViewController = [self.controllers objectAtIndex:row];
    [self.navigationController pushViewController:nextViewController animated:YES];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return NO;
}

@end

基本的に、2 つのテーブルを 2 つの異なるセクションに分けようとしている人がいますが、何を試してもうまくいかないようです。私のコードがあります。どこが間違っているのか教えてください。例を教えていただければ幸いです。誰もこれを解決できず、私はしばらくの間本当に行き詰まりました。ありがとうサム・ホートン

4

2 に答える 2

2

あなたのコードにはたくさんの問題があります。

  1. 次回はコードをフォーマットしてください。:)
  2. いくつかの場所で、これを行っています:

    DisclosureDetailController *th = [DisclosureDetailController alloc];
    ...
    SecondVoiceController *array2 = [SecondVoiceController alloc];
    ...
    ThirdVoiceController *array3 = [ThirdVoiceController alloc];
    

    オブジェクトを初期化せずに使用してはなりません。メソッドを使用する前に、メソッド (または適切な初期化子)を呼び出さなければなりませんinit

  3. プロパティは に初期化されnilます。NSMutableArrayそれらが指すはずのオブジェクトを作成することは決してありません。したがって、[male count]and[female count]は常に 0 を返します。つまり、単にプロパティを宣言することは、プロパティを初期化することと同じではありません。
于 2011-06-22T21:49:23.470 に答える
1

明らかに、書式設定のためにコードが少し読みにくいので、何かを読み違えたらごめんなさい。ただし、オブジェクトを追加する前にmale = [[NSMutableArray alloc] init];andを呼び出す必要があります。female = [[NSMutableArray alloc] init];配列が割り当てられていない場合、配列は に応答しませんaddObject。を呼び出すときにエラーが発生するaddObjectことはありません。最初に割り当てて初期化していない場合は、エラーが発生しません。

于 2011-06-22T21:28:02.973 に答える