0

I am trying to load a custom UIView over my ViewController , this UIView loads with a xib file and has a tableView , so when I connect dataSource and delegate with its file's owner and when I try to lunch the app , it crashes due to this message :

 TABLE VIEW LOAD  
 -[DinoViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to
instance 0x993b070  *** Terminating app due to uncaught exception

 'NSInvalidArgumentException', reason: 
'-[DinoViewController     tableView:numberOfRowsInSection:]: unrecognized selector sent to
 instance 0x993b070'
 *** First throw call stack:

here is my code :

//DinoViewController

- (IBAction)searchDino:(id)sender {

        DinoTable* tableview = [[DinoTable alloc]initWithFrame:CGRectMake(0,0,384,1024)];
        NSArray * dinoTableView = [[NSBundle mainBundle] loadNibNamed:@"DinoTable" owner:self options:nil];
        tableview = dinoTableView[0];
        [self.view addSubview:tableview];
    }

.TableView:

//  DinoTable.h
@interface DinoTable : UIView <UITableViewDataSource , UITableViewDelegate>  {

    NSArray *list;
}

//  DinoTable.m



- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {


        NSString *filePath = [[NSBundle mainBundle]pathForResource:@"dinoName" ofType:@"plist"] ;
        list = [NSArray arrayWithContentsOfFile:filePath];
        NSLog(@"TABLE VIEW LOAD");


    }
    return self;
}




 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {



 }


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 0;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return list.count;
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [list objectAtIndex:indexPath.row];

    return cell;
}
rmaddy
  • 298,130
  • 40
  • 468
  • 517
Mc.Lover
  • 5,536
  • 21
  • 82
  • 152

1 Answers1

0

From the code, DinoTable is a UIView and has no UITableView. I'm guessing you're pointing the datasource and delegate to DinoTable based on the error. These should point to a UITableView.

Add a UITableView inside DinoTable and add methods that your class can set the delegate/datasource with.

LyricalPanda
  • 1,394
  • 12
  • 25
  • I add UITableView in the UIView's xib file , by the way I add these codes : `[_tableView setDataSource:self]; [_tableView setDelegate:self];` but nothing changed ! – Mc.Lover Jun 04 '14 at 19:46
  • Try setting the delegate in the .h file Like this example http://stackoverflow.com/questions/11265039/set-uitableview-delegate-and-datasource – Maurice Jun 04 '14 at 20:15
  • @Mc.Lover Can you post your code for DinoViewController? I noticed the error is coming from there. You're setting the `tableView` to `DinoView`. Try unlinking the datasource and delegate from the XIB. I think you have them pointing to a UIView still. – LyricalPanda Jun 04 '14 at 20:24
  • I unlinked `datasource` and `delegate` from `file's owner` it works , but how can display data on tableview ?!! there is no code in DinoViewController , just a button that open the custom UIView ,check this line of code : `- (IBAction)searchDino:(id)sender` – Mc.Lover Jun 05 '14 at 07:44
  • That's because you're linking the datasource and delegate to the `UIViewController`, not where the `UITableView` is. Take a look at the link at the end of this comment (first answer). Add `setUpTableView` to `DinoTable` then call `[tableView setUpTableView]` in `DinoViewController` right after you instantiate `DinoTable *tableview`. http://stackoverflow.com/questions/18970013/uitableview-delegate-and-datasource-methods-not-getting-called – LyricalPanda Jun 05 '14 at 14:19
  • Alternatively you can also move your `UITableViewDelegate` and `UITableViewDataSource` to `DinoViewController` and put the delegate methods on `DinoViewController`, so that it controls the tableView in `DinoView` from the VC. – LyricalPanda Jun 05 '14 at 14:24