2012-04-15 43 views
0

提前致谢。使用NSArray与ARC的iOS TableView层次结构,如何重新填充数组?

目前正在研究一个由包含两个视图控制器的标签栏控制器组成的大学申请,第一个是无关紧要的,第二个是导航控制器。该导航控制器由一个包含用户可以选择的各种校园建筑物(来自NSArray)的tableView组成。导航控制器还包含另一个称为BuildingFloorsViewController的视图控制器本身有另一个表格本身,其中包含所选建筑物的NSArray中称为地面(地面,一楼等)的各种楼层。

所以这个想法是,相同的NSArray(地板)将重新填充取决于选择哪个建筑物。这是一个坏主意吗?

目前发生的情况是,表格采用前一个表格中的哪一行被选中的形式 - 这是非常有意义的,因为表格是在那里启动的。因此,例如,如果我先选择London Road行,它将创建具有4个值(+ nil)的数组,但如果我先选择Faraday Ring行,它将创建具有3个值(+ nil)的数组。

那么,我该如何去为每个if语句条件提供一组不同的值呢?我看过使用可变数组,只是调整值,但有另一种方式? 谢谢。

下面是解决了BuildingFloorsViewController.h

#import <UIKit/UIKit.h> 

@interface BuildingFloorsViewController : UITableViewController{ 
    NSArray *floors; 
    NSArray *londonRoadFloors; 
} 
@property (nonatomic, retain) NSArray *floors; 
@property (nonatomic, retain) NSArray *londonRoadFloors; 

@end 

的代码和BuildingFloorsViewController.m

#import "BuildingFloorsViewController.h" 

@interface BuildingFloorsViewController() 

@end 

@implementation BuildingFloorsViewController 
@synthesize floors; 
@synthesize londonRoadFloors; 

-(void)viewWillAppear:(BOOL)animated { 
if([self.title isEqualToString:@"Farady Wing"]){ 
    floors =[[NSArray alloc] 
      initWithObjects:@"First Floor",@"Second Floor",@"Third Floor",nil]; 
}else if([self.title isEqualToString:@"London Road"]){ 
    floors =[[NSArray alloc] 
      initWithObjects:@"Ground Floor",@"First Floor",@"Second Floor",@"Third Floor",nil]; 
    } 
} 


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 

    if (self) { 
    // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
// Return the number of sections. 
return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
// Return the number of rows in the section. 
return [floors count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
} 

cell.textLabel.text=[self.floors objectAtIndex:[indexPath row]]; 
return cell; 
} 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
//<#RoomViewController#> *roomViewController = [[<#RoomViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil]; 

} @end

+0

究竟是什么问题? – 2012-04-15 00:09:30

+0

如何更改每个不同If语句的NSArray值,我将编辑该问题。 – 2012-04-15 00:34:01

+0

你不是已经在'viewWillAppear'中做这个了吗? – lnafziger 2012-04-15 01:13:35

回答

0

!我省略了[self.tableView reloadData]。花了我一段时间才弄清楚 - 我不得不回去逐行分析它。

下面是它应该如何。

-(void)viewWillAppear:(BOOL)animated { 
if([self.title isEqualToString:@"Farady Wing"]){ 
    floors =[[NSArray alloc] 
      initWithObjects:@"First Floor",@"Second Floor",@"Third Floor",nil]; 
}else if([self.title isEqualToString:@"London Road"]){ 
    floors =[[NSArray alloc] 
      initWithObjects:@"Ground Floor",@"First Floor",@"Second Floor",@"Third Floor",nil]; 
    } 
[super viewWillAppear:animated]; 
[self.tableView reloadData]; 
} 

非常感谢社区,但我很感谢你看着这个问题,我希望这可以帮助别人。