2

我创建了一个带有设置表单的 iPad 应用程序。表单使用两个子视图来创建像显示一样的拆分视图。左侧(主) UITableView 滚动并正常启动。The error is coming in from when one of the master cells is selected and it displays a UITableView from a UITableViewController into the right(detail) view. 它将显示 UITableView,但一旦滚动,详细视图就会崩溃。是的,我有从 plist 中提取的列表,因为我希望以后可以在应用程序的其他位置访问这些结果。

这是表单的视图控制器和主视图 .h 文件的委托

@interface settingsViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>{
    UITableView *mainTableView;
    NSDictionary *mainSettingList;
    NSArray *settingItems;
}

@property (weak, nonatomic) IBOutlet UINavigationBar *settingsNavigationBar;
@property (weak, nonatomic) IBOutlet UITableView *mainTableView;
@property (weak, nonatomic) IBOutlet UIView *borderView;
@property (strong, nonatomic) IBOutlet UIView *detailView;

.m 文件

@property (nonatomic, strong)NSDictionary *mainSettingList;
@property (nonatomic, strong)NSArray *settingItems;

@end

@implementation settingsViewController
@synthesize settingsNavigationBar = _settingsNavigationBar;
@synthesize mainTableView = _mainTableView;
@synthesize borderView = _borderView;
@synthesize settingItems, mainSettingList;



-(void)viewDidLoad{
    [super viewDidLoad];
    [self.detailView setFrame:CGRectMake(233, 0, 310, 620)];
    [self.borderView setBackgroundColor:[UIColor lightGrayColor]];
    [self.view addSubview:self.borderView];
    [self.view addSubview:self.detailView];

}

- (IBAction)closePressed:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(NSDictionary *)mainSettingList{
    if(!mainSettingList){
        NSString *str_settingList = [[NSBundle mainBundle]pathForResource:@"settingsList" ofType:@"plist"];
        mainSettingList = [NSDictionary dictionaryWithContentsOfFile:str_settingList];
        self.mainSettingList = mainSettingList;
    }
    return mainSettingList;
}

-(NSArray *)settingItems{
    if(!settingItems){
        settingItems = [[self.mainSettingList allKeys]sortedArrayUsingSelector:@selector(compare:)];
        self.settingItems = settingItems;
    }
    return settingItems;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.settingItems.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *wordsInSection = [self.mainSettingList objectForKey:
                                [self.settingItems objectAtIndex:section]];
    return wordsInSection.count;
}


-(NSString *)nameAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *wordsInSection = [self.mainSettingList objectForKey:[self.settingItems objectAtIndex:indexPath.section]];
    return [wordsInSection objectAtIndex:indexPath.row];
}


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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    cell.textLabel.text = [self nameAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [self.settingItems objectAtIndex:section];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UINavigationController *nvc = [[UINavigationController alloc]init];
    [nvc.view setFrame:CGRectMake(0,0, 310, 620)];
    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *cellText = selectedCell.textLabel.text;

    if([cellText isEqualToString:@"Types of Jumps"]){
        TypesOfJumps *toj = [[TypesOfJumps alloc]initWithNibName:@"TypesOfJumps" bundle:nil];
        [nvc pushViewController:toj animated:YES];
    }
    [self.detailView addSubview:nvc.view];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[NSString stringWithFormat:@"You selected %@!", cellText] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

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

- (void)viewDidUnload {
    [self setSettingsNavigationBar:nil];
    [self setBorderView:nil];
    [self setMainTableView:nil];
    [self setSubViewNavigaionBar:nil];
    [self setDetailView:nil];
    [super viewDidUnload];
}
@end

单元格按下方法调用的 UITableViewController .h 文件

@interface TypesOfJumps : UITableViewController<UITableViewDataSource,UITableViewDelegate>{
    NSDictionary *listJumps;
    NSArray *typesJumps;
}
@property (strong, nonatomic) IBOutlet UITableView *tableView;

.m 文件

@interface TypesOfJumps ()
@property (strong, nonatomic)NSDictionary *listJumps;
@property (strong, nonatomic)NSArray *typesJumps;

@end

@implementation TypesOfJumps
@synthesize listJumps = _listJumps;
@synthesize typesJumps = _typesJumps;
@synthesize tableView = _tableView;

-(NSDictionary *)listJumps{
    if(!listJumps){
        NSString *str_settingList = [[NSBundle mainBundle]pathForResource:@"TypesOfJumps" ofType:@"plist"];
        listJumps = [NSDictionary dictionaryWithContentsOfFile:str_settingList];
        self.listJumps = listJumps;
    }
    return listJumps;
}

-(NSArray *)typesJumps{
    if(!typesJumps){
        typesJumps = [[self.listJumps allKeys]sortedArrayUsingSelector:@selector(compare:)];
        self.typesJumps = typesJumps;
    }
    return typesJumps;
}



- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.typesJumps.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *wordsInSection = [self.listJumps objectForKey:
                               [self.typesJumps objectAtIndex:section]];
    return wordsInSection.count;
}


-(NSString *)nameAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *wordsInSection = [self.listJumps objectForKey:[self.typesJumps objectAtIndex:indexPath.section]];
    return [wordsInSection objectAtIndex:indexPath.row];
}


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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    cell.textLabel.text = [self nameAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [self.typesJumps objectAtIndex:section];
}
4

0 回答 0