1

次のファイルで構成されるビューベースのアプリケーションがあります

  1. ボタンを持つメインView Controller

  2. 一連の国を表形式で表示する表示ビュー コントローラ

  3. 2 番目のステップで選択した国の一連の州をテーブル形式で再度表示する states ビュー コントローラー

それはボタンです --> countries(tableview)セルを選択した後--> states(tableview)

特定の国をクリックしたときにその国の州に移動できないことを除けば、すべて正常に機能します。例えば。AUS を選択すると、AUS の状態が表示されません。

コードは次のとおりです。

ボタンを持つメイン ビュー コントローラー:

.h ファイル

    { 
     IBOutlet UIButton *btn;
    }
    -(IBAction)click;

.m ファイル

    -(IBAction)click
        {

            display *dis=[[display alloc]initWithNibName:@"display"bundle:nil];
            [self presentModalViewController:dis animated:YES];

        }

ディスプレイ ビュー コントローラ

.h ファイル

                    @interface display : UITableViewController
        {
            NSArray *countries;
        }
        @property(nonatomic,retain)NSArray *countries;

        @end

display.m ファイル

                - (void)viewDidLoad {
                   NSArray *count=[[NSArray alloc] initWithObjects:@"India",@"USA",@"UK",@"AUS",@"SA",@"PAK",@"BANGLADESH",nil];
                    self.countries=count;
                [count release];
                self.countries=count;
                    self.title=@"Select a Country";
                    [super viewDidLoad];

                }

                - (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 [countries count];
                }


                // Customize the appearance of table view cells.
                - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

                    static NSString *CellIdentifier = @"Cell";



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

                    // Configure the cell...
                    //countries=
                    cell.textLabel.text=[countries objectAtIndex:indexPath.row];
                    return cell;
                }

                - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
                    // Navigation logic may go here. Create and push another view controller.
                    //UITableViewCell *thiscell=[tableView cellForRowAtIndexPath:indexPath];
                    if(indexPath.row==0)
                    {   
                     states *detailViewController = [[states alloc] initWithNibName:@"states" bundle:nil];
                     // ...
                     // Pass the selected object to the new view controller.
                        //detailViewController.indexPath=self.indexPath;
                        //self.dis.indexValue=indexPath;
                     [self.navigationController pushViewController:detailViewController  animated:YES];
                        //detailViewController.indexPath=self.indexPath;
                        [self presentModalViewController:detailViewController animated:YES];

                     [detailViewController release];
                    }
                    if(indexPath.row==1)
                    {
                        states *detailViewController = [[states alloc] initWithNibName:@"states" bundle:nil];
                        // ...
                        // Pass the selected object to the new view controller.
                        [self.navigationController pushViewController:detailViewController animated:YES];
                        [self presentModalViewController:detailViewController animated:YES];

                        UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
                        [detailViewController release]; 

                    }

                    [tableView deselectRowAtIndexPath:indexPath animated:NO];
                }

statesenter コードをここにコントローラー

states.h ファイル

            @interface states : UITableViewController {
                NSArray *state;
                NSArray *state1;
                NSArray *state2;
                //NSIndexPath *indexPath;
            }
            @property(nonatomic,retain) NSArray *state;
            @property(nonatomic,retain) NSArray *state1;
            @property(nonatomic,retain) NSArray *state2;
            //@property(nonatomic,retain)display *dis;
            //@property(nonatomic,retain)NSIndexPath *indexPath;
            @end

states.m ファイル

                     - (void)viewDidLoad {


                        NSArray *sta=[[NSArray alloc]initWithObjects:@"goa",@"ap",@"karnataka",nil];
                        self.state=sta;
                        [sta release];
                        self.state=sta;
                        [super viewDidLoad];

                            NSArray *sta1=[[NSArray alloc]initWithObjects:@"Los Angeles",@"California",@"Las Vegas",nil];
                            self.state1=sta1;
                            [sta release];
                            self.state1=sta1;
                            [super viewDidLoad];

                            NSArray *sta2=[[NSArray alloc]initWithObjects:@"Southhampton",@"Nottingham",@"London",nil];
                            self.state2=sta2;
                            [sta release];
                            self.state2=sta2;
                            [super viewDidLoad];



                        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
                        // self.navigationItem.rightBarButtonItem = self.editButtonItem;
                    }


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



                    // Customize the appearance of table view cells.
                    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

                        static NSString *CellIdentifier = @"Cell";

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

                        // Configure the cell...
                        if(indexPath.row==0)
                        {
                        cell.textLabel.text=[state objectAtIndex:indexPath.row];
                        }
                        else if(indexPath.row==1)
                        {
                            cell.textLabel.text=[state1 objectAtIndex:indexPath.row];

                        }
                        return cell;
                    }

したがって、このコードを実行すると問題なく実行されますが、国を選択するたびにサウスハンプトンとカリフォルニアが表示されます。

すべての状態に対して複数のView Controllerを作成したくありません。そうするのはばかげています。そこで、配列を作成し、状態ビュー コントローラーを使用して、選択した国のさまざまな状態配列を表示します。

indexpathそれぞれの国のすべての州にアクセスできるように、どのように国の値を州のView Controllerに渡す必要がありますか? (注: 状態のすべての配列を作成したわけではありません。確認するために 2 つ作成しただけです。)

4

2 に答える 2

0

まず、ディスプレイ ビュー コントローラーの didSelectRowAtIndexPath で indexPath を比較している理由は何ですか?このようなコードを配置することもできます。indexPathのみを渡したい場合:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //UITableViewCell *thiscell=[tableView cellForRowAtIndexPath:indexPath];
    states *detailViewController = [[states alloc] initWithNibName:@"states" bundle:nil];
    //Pass the selected object to the new view controller.
    detailViewController.m_SelectedIndexPath=indexPath;
    [self.navigationController pushViewController:detailViewController  animated:YES];        [self presentModalViewController:detailViewController animated:YES];
    [detailViewController release];

[tableView deselectRowAtIndexPath:indexPath animated:NO];
 }

states.m では、cellForRowAtIndexPath で m_SelectedIndexPath を次のように確認できます。

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

 // Configure the cell...
 if(m_SelectedIndexPath == 0)
 {
     cell.textLabel.text=[state objectAtIndex:indexPath.row];
 }
  else if(m_SelectedIndexPath==1)
 {
     cell.textLabel.text=[state1 objectAtIndex:indexPath.row];
 }
  return cell;
 }

プッシュするだけでなく、テーブルビューを提示する2番目のこと。プッシュまたは提示する必要があります。

于 2012-06-04T12:27:53.277 に答える
0

ねえ、countriesViewのdidSelectRowAtIndexPathデリゲートメソッドでstateViewのsetTableArrayを使用してください...

次のような setTableArray メソッド:

-(void)setTableArray:(NSMutableArray *)arr
{
    arrData = arr;
    [arrData retain];
    //[tblView reloadData];
}

ここで arrData は MutableArray です....

そのときにdidSelectRowAtIndexPathからstateViewをプッシュすると、次のようにaarrayを設定するだけです...

[detailViewController setTableArray:yourArray];

ここでは、選択した国のインデックスでデータを取得し、その配列を yourArray に渡すだけです...うまくいけば、これはあなたを助けます... :)

于 2012-06-04T12:24:18.243 に答える