0

マスター/ディテール アプリがあり、マスターの行をクリックして新しいビューをロードしたいと考えています。行 1 を選択するとビューが異なり、別の行を選択すると、詳細部分に新しいビューが表示されます。どうすればこのことを達成できますか?

私はこのようなことをしています。問題は、これらの 2 つの行のいずれかを初めて選択すると、ビューが実際に変更されることです。しかし、もう一度他の行をクリックしても変わりません。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

if(indexPath.row==0){


    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 

    TwitterTweetViewController *detailView=(TwitterTweetViewController *)[storyboard instantiateViewControllerWithIdentifier:@"TwitterDetailView"];

    self.splitViewController.mainViewController = detailView;
    [self.splitViewController.mainViewController viewDidLoad];
    [self.splitViewController viewDidLoad];

}


if(indexPath.row == 1)
{

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 

    TwitterDetailViewController *detailView=(TwitterDetailViewController *)[storyboard instantiateViewControllerWithIdentifier:@"TwitterDetailView"];

    self.splitViewController.mainViewController = detailView;
    [self.splitViewController.mainViewController viewDidLoad];
    [self.splitViewController viewDidLoad];

}

}

これらの行を選択する際のビューを変更するにはどうすればよいですか?

4

3 に答える 3

0

You can create an enum of your view controllers as :-

enum MyViewControllers
{
viewcontroller1
viewcontroller2
viewcontroller3
}

The constatns in enum would correspond to your viewcontrollers for each indexPAth.

and then in the delegate method of UITAbleVIew didSelectRowAtIndexPath: , use switch case :-

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath 
    {
    switch(indexPath.row)
{
    case 1:
          Load view1
          break;
    case 2:
          Load view2
          break;
    case 3:
          Load view3
          break;     .
         .
         so on.
    }
}

Or in case of different NavigationControllers (as in iPad )you will have to pass data using delegates and protocol.Define the protocol in rootViewController and then set its delegate in the detail(Where you want to push the viewcontroller)

somethinglike :-

@protocol SendSelectedINdexDelegate
{
@required
-(void)sendTheIndex:(NSNumber)number.
@end

in interface

id <SendSelectedINdexDelegate> delegateSend

set its property:-

@property (nonatomic) id delegateSend;

in .m

@synthesize delegateSend

and in didSelectRowAtIndexPath:-

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
[self delegate]sendTheIndex:indexPath.row];

}

At the receiving controller conform to protocol and set

rootview.delegateSend=self;

and implement the method of protocol;

-(void)sendTheIndex:(NSNumber)number
{
//nsnumber is your index perform operation based on this.
}
于 2012-05-14T07:35:25.397 に答える
0

素晴らしい方法があります:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

これを使って。セルをクリックした直後に呼び出されます。そこにチェックを入れることができます。短い例:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  if (indexPath.row == 0) {
    SomeViewController *svc = [[SomeViewController alloc]initWithNibName:@"SomeViewController" bundle:nil];
    [self.navigationController pushViewController:svc];
  } else if (indexPath.row > 0 && indexPath.row < 5) {
    SomeOtherViewController *sovc = [[SomeOtherViewController alloc]initWithNibName:@"SomeOtherViewController" bundle:nil];
    [self.navigationController pushViewController:sovc];
  } else {
    AnotherViewController *avc = [[AnotherViewController alloc]initWithNibName:@"AnotherViewController" bundle:nil];
    [self.navigationController pushViewController:avc];
  }
  //some other code if needed
}

それが役に立てば幸い

于 2012-05-14T07:27:40.737 に答える