可能重复:
iPhone UITableView分区
基本上,尝试两个的人把表格分为两个不同的部分,但无论我尝试什么,它似乎都不起作用,这是我的代码,请你告诉我哪里出了问题,示例将不胜感激!还没有人能解决这个问题,我真的被困了一段时间。谢谢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
您的代码有很多错误。
- 请下次格式化您的代码。:)
-
在一些地方,你正在这样做:
DisclosureDetailController *th = [DisclosureDetailController alloc]; ... SecondVoiceController *array2 = [SecondVoiceController alloc]; ... ThirdVoiceController *array3 = [ThirdVoiceController alloc];
您必须在不初始化对象的情况下永远不要使用对象。在使用对象之前,必须调用
init
方法(或适当的初始化器)。 - 您的属性已初始化为
nil
。您永远不会创建它们应该指向的NSMutableArray
对象。因此,[male count]
和[female count]
将始终返回0。换句话说,简单地声明一个属性与初始化它不是一回事
显然,由于格式的原因,代码有点难以阅读,所以如果我误读了什么,我很抱歉。但是,在向male = [[NSMutableArray alloc] init];
和female = [[NSMutableArray alloc] init];
添加任何对象之前,需要调用它们。如果未分配阵列,它们将不会响应addObject
。调用addObject
时不会出现任何错误,只是如果您没有首先对其进行分配和初始化,它就不会出错。