【问题标题】:didSelectRowAtIndexpath method for root / tableView in splitviewCotrollersplitviewCotroller 中 root / tableView 的 didSelectRowAtIndexpath 方法
【发布时间】:2011-12-26 13:05:26
【问题描述】:
  • 我在我的项目中使用拆分视图控制器,
  • 在拆分视图中,我有 RootViewCotroller(左), 详细信息ViewConroller(右)。
  • 在根视图控制器中,我有 3 行。
  • 现在我想在细节视图中显示不同类型的外观,当 在 rootview 中选择不同的行。
  • ...
  • 在详细信息视图中:
  • 1.选择row1时显示tableView。
  • 2.选择行2时显示文本字段。
  • 3.从rootviewcontroller中选择row3时的图像视图。
  • ..
  • 我在 RootViecontroller 中编写了这样的代码,但在 详情查看。
  • -
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

       {
           RightViewController *rightViewController = [[RightViewController alloc] initWithNibName:@"RightViewController"bundle:nil];
           //[self.navigationController pushViewController:rightViewController animated:YES];

           if(indexPath.row == 0)
           {
               UILabel *infoLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 450, 150)];
               infoLabel.text = @"Customer:Barrick \r\n Mine:gold \r\n Location:USA";
               [rightViewController.view addSubview:infoLabel];

               UITableView *mapTable = [[UITableView alloc]initWithFrame:CGRectMake(165, 5, 450,150)style:UITableViewStyleGrouped];
               [rightViewController.view addSubview:mapTable];
           }

           if(indexPath.row == 1)
           {
               UILabel *infoLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 450, 150)];
               infoLabel.text = @"Eqipement";
               [rightViewController.view addSubview:infoLabel];
           }


           if(indexPath.row == 2)
           {
             image view..   
           }

           [rightViewController release];
       }
  • 怎么办..?提前致谢。

【问题讨论】:

    标签: iphone uitableview uisplitviewcontroller ipad


    【解决方案1】:

    您必须将值传递给右视图控制器,并且还必须为右视图控制器设置委托。

    在左视图控制器中定义右视图控制器,例如:

    RightViewController *detail;
      /// in implementation of left view contrloller
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
        NSUInteger item = [indexPath row] +1;
        NSNumber *detailItem = [NSNumber numberWithInteger:item];
        self.detailCar.detailItem = detailItem;
    }
    

    在您的 rightViewController.h 文件中定义:

    @property (retain, nonatomic) NSNumber *detailItem;
    

    在 RightViewController 的实现文件中:

    - (void)setDetailItem:(NSNumber *)newDetailItem {
        NSLog(@"ConfigTItem");
        if (_detailItem != newDetailItem) {
            [_detailItem release]; 
            _detailItem = [newDetailItem retain]; 
            [self configureView];
        }
        else {
            [self configureView]; 
        }
    }
    
    - (void)configureView {        
        if (self.detailItem) {
            [self viewDidLoad];  // 
            //set your method's and variable whatever you want to do in your view did load or directly here
        }
    }
    

    【讨论】:

    • 它正在工作,但有一个小问题我使用了如下代码
    • if( detailItem == [NSNumber numberWithInt:1]) { NSLog(@"在第 1 行配置项目 %@",detailItem); UILabel *infoLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 450, 150)]; infoLabel.text = @"客户:Barrick Gold\r\n矿山:Turquiose Ridge 矿山 \r\n位置:亚利桑那州梅萨"; [self.view addSubview:infoLabel]; }
    • if( detailItem == [NSNumber numberWithInt:2]) { NSLog(@"第 2 行的配置项 %@",detailItem); UITableView *mapTable = [[UITableView alloc]initWithFrame:CGRectMake(165, 5, 450,150 style:UITableViewStyleGrouped]; [self.view addSubview:mapTable]; } if( self.detailItem == [NSNumber numberWithInt:3]) { NSLog( @"第 3 行的配置项 %@",detailItem); }
    • 它正在工作,但有一个小问题我使用了如下代码
    • 这里 NSLog 在控制台中工作.....但是子视图像..:标签,UITableView 没有添加到 detailsview 中是什么问题..
    【解决方案2】:

    不要尝试从另一个控制器渲染视图。单击行时,将行的索引值传递给 rightView 控制器。然后根据 rightView 控制器中的行值渲染视图。

    【讨论】:

    • 感谢您的快速回复。那么现在如何将 indexvalue 传递给右视图控制器?
    • 在右视图控制器中为行说整数类型的 indexRowVal 创建一个属性,并在根控制器中将值分配给它作为 rightViewController.indexRowVal = [indexPath row]。
    • 你甚至可以创建一个方法来初始化并从 rootView 控制器调用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    • 1970-01-01
    • 2012-01-06
    • 1970-01-01
    相关资源
    最近更新 更多