【问题标题】:UITableView using xib file, NSObject subclass and using in UIViewControllerUITableView 使用 xib 文件、NSObject 子类并在 UIViewController 中使用
【发布时间】:2016-04-23 13:55:21
【问题描述】:
  1. 我在Xcode 中创建了一个单视图项目,默认情况下包含ViewController 文件。
  2. 我创建了一个名为MyClassNSObject 子类。
  3. 我在MyClass#import <UIKit/UIKit.h>
  4. MyClass 确认 UITableViewDelegateUITableViewDatasource
  5. 实现了UITableViewDelegateUITableViewDatasource所需的方法
  6. 我创建了一个xib 文件,并在UIView 中拖放了UITableView
  7. 我将xib文件files owner类设置为MyClass
  8. 在 xib 文件中设置 UITableView 委托和数据源。
  9. ViewControllerviewDidAppear我做到了

    UIView *subView1=[[[NSBundle mainBundle] loadNibNamed:@"View1" owner:self options:nil] objectAtIndex:0];
    MyClass *c = [[MyClass alloc]init];
    [self.view addSubview:subView1];
    

但我在[self.view addSubview:subView1]; 收到此错误

-[ViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x10027adc0

在 xcode 控制台中我做了po 0x10027adc0,即ViewController

我正在尝试使用 xib fileNSObject 子类创建 UITableView 的功能,以便我可以在 UIViewController 类中的任何地方重用它。

我做错了什么?

【问题讨论】:

  • 你写numberOfRowsInSection方法了吗?
  • @the_UB 是的,我收到错误消息的 numberOfRowsInSection 方法。
  • 显示numberOfRowsInSection方法。

标签: ios objective-c uiviewcontroller xib nsobject


【解决方案1】:

当您创建了一个名为 MyClass 的 NSObject 子类时。您还与 TableView Delegate & Datasource 绑定。但它是一种 NSObject。而不是创建UIViewUIViewController 类型类。

而且,如果您想构建一次代码并多次使用它,请改为创建UITableViewController 的子类或使用扩展方法生成UITableViewController 的Category 类。

【讨论】:

  • 我的要求不仅限于 uitableview,所以我需要这个解决方案来处理多个文件,我将把这些文件作为我的大型复杂应用程序的一部分。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-23
  • 2011-01-05
  • 1970-01-01
  • 1970-01-01
  • 2012-06-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多