2012-04-15 88 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]; 
} 

非常感謝社區,但我很感謝你看着這個問題,我希望這可以幫助別人。