【问题标题】:UITableView and delegatesUITableView 和委托
【发布时间】:2011-06-16 09:58:22
【问题描述】:

我正在开发一个 iPad 应用程序,但我很难在我的表格视图中捕捉到对一行的选择。我知道这是因为我还没有正确定义我的代表,但是在网上 2 小时后,它仍然没有多大意义。 我想要做的是将选定的表格行项目传递给一个新视图,该视图根据选择显示信息 - 非常标准。 我使用选项将 tableViewController 子类创建为 UITableViewController 子类,除非我错了,否则它会自动合并委托(UITableViewDelegate 和 UITableViewDataSource)。

在 didSelectRowsAtIndex 方法中,我试图创建一个 DetailViewController。我尝试过使用 nib 文件并纯粹在代码中创建一个,但从未创建过该类。我错过了一个我确定的步骤,但我找不到它是什么。在某些时候,我不应该定义要使用所选行访问的功能吗?但是哪里?怎么样?

在我认为是我最好的尝试中,我创建了 DetailViewController,将 detailViewController 中的字符串变量设置为选定的行,然后尝试添加要显示的 detailViewController 视图。我想我可以使用 viewDidload 调用下一个函数,但视图从未显示在屏幕上。

一些基本的指导会很好。或者一个像样的教程会很好。请不要致电阅读相关文档,我已经完成了,现在我需要一个示例来将所有内容整合在一起。 谢谢, 史蒂夫

【问题讨论】:

  • 考虑编辑您的问题并发布您迄今为止尝试过的代码。
  • 不确定我做了什么来解决这个问题,但我删除了我所做的并再次尝试。很确定我没有更改任何重大内容,但委托现在正在调用该类的 viewDidLoad。令人沮丧...

标签: objective-c delegates uitableview


【解决方案1】:

我认为您在 didSelectRowAtIndexPath: 方法中缺少这一行

[self presentModalViewController:controller animated:YES];

其中控制器是类DetailViewController的对象

【讨论】:

    【解决方案2】:

    是的,也许粘贴代码 sn-p 会更容易弄清楚这里发生了什么。那些 delegate(didSelectRowAtIndexPath:) 方法是否被正确调用?

    【讨论】:

      【解决方案3】:

      试试这个,

      这在 didSelectRowAtIndexPath 中

       MoreInfoTable *moreInfoView = [[MoreInfoTable alloc] initWithStyle:UITableViewStyleGrouped];
      //in the MoreInfoTable, make properties like titles etc.
      [self.navigationController pushViewController:moreInfoView animated:YES];
      [moreInfoView release]; 
      }
      

      这是一个 MoreInfoTable.h 的示例

         @interface MoreInfoTable : UITableViewController {
      NSMutableArray *moreInfo;
      NSURL *getDirections;
      NSURL *getWebsite;
      NSMutableString *getPhoneNumber;
      NSString *address;
      NSString *footer;
      float lat, lon;
      
      
      }
      -(void)goToWebsite;
      -(void)goToMaps;
      -(IBAction)addToFavorites:(id)sender;
      -(void) callNumber;
      @property (nonatomic,retain) NSURL *getDirections;
      @property (nonatomic,retain) NSURL *getWebsite;
      @property (nonatomic,retain) NSMutableString *getPhoneNumber;
      @property (nonatomic,retain) NSString *footer;
      @property (nonatomic,retain) NSString *address;
      @property (readwrite) float lat;
      @property (readwrite) float lon;
      @end
      

      现在回到你声明表的另一个文件中,你可以说

      MoreInfoTable *moreInfoView = [[MoreInfoTable alloc] initWithStyle:UITableViewStyleGrouped];
      //in the MoreInfoTable, make properties like titles etc.
      moreInfoView.title = @"TITLE!";
      //etc. 
      [self.navigationController pushViewController:moreInfoView animated:YES];
      [moreInfoView release];  //
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-11
        • 2012-07-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-15
        • 2014-03-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多