0

前もって感謝します。

現在、2 つのビュー コントローラーを含むタブ バー コントローラーで構成される大学向けのアプリケーションに取り組んでいます。1 つ目は無関係で、2 つ目はナビゲーション コントローラーです。そのナビゲーション コントローラーは、ユーザーが選択できる (NSArray からの) さまざまなキャンパス ビルディングを含む tableView で構成されます。ナビゲーション コントローラには、BuildingFloorsViewController と呼ばれる別のビュー コントローラも含まれています。それ自体には、選択した建物のフロア (1 階、1 階など) と呼ばれる 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];

} @終わり

4

1 に答える 1

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];
}

コミュニティに感謝しますが、この質問を見ていただきありがとうございます。これが他の人の助けになることを願っています。

于 2012-04-15T14:48:05.367 に答える