-2
  1. I created a single view project in Xcode which by default includes ViewController file.
  2. I created a NSObject subclass named MyClass.
  3. I #import <UIKit/UIKit.h> in MyClass.
  4. MyClass confirms to UITableViewDelegate and UITableViewDatasource
  5. Implemented the required methods of UITableViewDelegate and UITableViewDatasource
  6. I created a xib file, and in UIView I dragged dropped the UITableView.
  7. I set the xib file files owner class to MyClass.
  8. Set the UITableView delegate and datasource in xib file.
  9. In ViewController viewDidAppear I did this

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

But I am getting this error on [self.view addSubview:subView1];

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

In xcode console I did po 0x10027adc0 which is ViewController

I am trying to create a functionality of UITableView using xib file and NSObject subclass so I can reuse it where ever I want in UIViewController class.

What I am doing wrong?

Cristik
  • 30,989
  • 25
  • 91
  • 127
S.J
  • 3,063
  • 3
  • 33
  • 66

1 Answers1

0

As you created a NSObject subclass named MyClass. As well you are binding with TableView Delegate & Datasource. But it is kind of NSObject. Instead of creating UIView or UIViewController type class.

And, if you want to build code for once & use it for multiple times, so please make subclass of UITableViewController instead or generate Category class of UITableViewController with extended methods.

Manann Sseth
  • 2,745
  • 2
  • 30
  • 50
  • My requirement is not limited to uitableview so I need this solution for multiple files which I will include as a part in my big complex app. – S.J Jan 18 '16 at 12:27