iOS TableView Hierarchy使用NSArray和ARC,如何重新填充数组?

时间:2012-04-15 00:01:22

标签: ios xcode nsarray tableview

提前致谢。

目前正在开发一个大学应用程序,它包含一个包含两个视图控制器的标签栏控制器,第一个是不相关的,第二个是导航控制器。该导航控制器包含一个tableView,其中包含用户可以选择的各种校园建筑(来自NSArray)。导航控制器还包含另一个名为BuildingFloorsViewController的View Controller本身有另一个表本身,其中包含NSArray中所选建筑物的楼层(底层,一层等)中的各种楼层。

因此,根据选择的建筑物,相同的NSArray(地板)将重新填充。这是一个坏主意吗?

目前发生的情况是表格选择了上一个表格中哪一行的形式 - 这在表格随后被启动时非常有意义。因此,例如,如果我首先选择伦敦路行,它将创建具有4个值(+ nil)的数组,但如果我首先选择法拉第环行,则将创建具有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

1 个答案:

答案 0 :(得分: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];
}

非常感谢社区,我感谢你看到这个问题,我希望这有助于其他人。