iPhone UITableView部分不起作用

时间:2011-06-22 21:19:42

标签: iphone objective-c xcode uitableview sections

  

可能重复:
  iPhone UITableView Sections

基本上尝试两个的人把桌子分成两个不同的部分,但无论我尝试它似乎都不起作用,有我的代码请你告诉我哪里出错了,例子将不胜感激!还没有人设法解决这个问题,我已经陷入了一段时间。谢谢Sam Houghton。

#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

基本上尝试两个的人把桌子分成两个不同的部分,但无论我尝试它似乎都不起作用,有我的代码请你告诉我哪里出错了,例子将不胜感激!还没有人设法解决这个问题,我已经陷入了一段时间。谢谢Sam Houghton

2 个答案:

答案 0 :(得分:2)

您的代码存在许多问题。

  1. 请在下次格式化您的代码。 :)
  2. 在某些地方,您正在这样做:

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

    您必须从不使用对象而不进行初始化。 必须在使用之前调用init方法(或相应的初始化程序)。

  3. 您的媒体资源已初始化为nil。你永远不会创建他们应该指向的NSMutableArray个对象。因此,[male count][female count]将始终返回0.换句话说,简单地声明属性与初始化属性不同。

答案 1 :(得分:1)

由于格式化,代码有点难以阅读,显然,如果我误读任何内容,我很抱歉。但是,在向其添加任何对象之前,您需要调用male = [[NSMutableArray alloc] init];female = [[NSMutableArray alloc] init];。如果未分配数组,则它们不会响应addObject。调用addObject时不会出现任何错误,如果你没有先分配并初始化它,它就不会这样做。