1

I have a UIViewController that I want to use to implement the methods of UITableViewDataSource on. I have this header, FriendsController.h:

#import <UIKit/UIKit.h>
@interface FriendsController : UIViewController <UITableViewDataSource>
@end

Edit: now updated the @interface declaration to:

@interface FriendsController : UIViewController <UITableViewDataSource, UITableViewDelegate>

and this implementation, FriendsController.m:

#import "FriendsController.h"

@implementation FriendsController

- (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section
{
  // Return the number of rows in the section.
  NSLog(@"CALLED .numberOfRowsInSection()");
  return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  NSLog(@"CALLED cellForRowAtIndexPath()");
  UITableViewCell *cell = [tableView
                             dequeueReusableCellWithIdentifier:@"FriendCell"];
  cell.textLabel.text = @"Testing label";
  return cell;
}
@end

When run this gives me a '-[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x81734d0'. Can anyone see if there is something wrong with my implementation/declaration of .numberOfRowsInSection()?

Edit: I've added a technique for method listing from here and run the view 'unconnected', it outputs the following list:

[<timestamp etc>] Method no #0: tableView:numberOfRowsInSection:
[<timestamp etc>] Method no #1: tableView:cellForRowAtIndexPath:
[<timestamp etc>] Method no #2: numberOfSectionsInTableView:
[<timestamp etc>] Method no #3: tableView:didSelectRowAtIndexPath:
[<timestamp etc>] Method no #4: viewDidLoad

Post scriptum: Both @ThisDarkTao and @Pei got it right, as can be seen in my previous question documenting the visual parts, here.

Community
  • 1
  • 1
Jacob Oscarson
  • 6,363
  • 1
  • 36
  • 46
  • 1
    Do you tried to add "numberOfSectionsInTableView"? –  May 28 '12 at 13:38
  • Tried it, but that but that isn't what's generating the exception, the exception complains about `.numberOfRowsInSection:`. – Jacob Oscarson May 28 '12 at 14:08
  • The key here is that the error above implies you are trying to pass a method to a UIView not a UIViewController. You have either connected the delegate and datasource connectors to the view, not the viewcontroller, or something very weird is happening. – ThisDarkTao May 28 '12 at 14:31
  • Wow, you were right on the last comment! I'll mark your anwser as right and make a little edit in the question. Thanks a bunch for all your time! – Jacob Oscarson May 28 '12 at 14:38

2 Answers2

1

You need to add UITableViewDelegate to the list of protocols in the interface file, like this: <UITableViewDataSource, UITableViewDelegate>

You also need all of the following delegate methods:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1; // Number of rows
}

- (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 = @"Test Cell";

    return cell;
}

In your xib/storyboard view, you also need to connect the tableview's delegate and datasource connections to the viewcontroller.

If you want your cells to 'do something' when you tap them, you also need to implement the following delegate method:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"Tapped cell %d",indexPath.row);     
}
ThisDarkTao
  • 1,062
  • 10
  • 27
  • This doesn't seem to be it - it's still complaining about the `tableView:numberOfRowsInSection:` method not being defined. – Jacob Oscarson May 28 '12 at 14:01
  • 2012-05-28 16:14:12.896 BwF[46212:10703] -[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7686fd0 2012-05-28 16:14:12.900 BwF[46212:10703] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7686fd0' – Jacob Oscarson May 28 '12 at 14:14
  • Also: it's a Storyboard-based project (if that's relevant) – Jacob Oscarson May 28 '12 at 14:15
  • Do you have your viewcontroller's class set in the view contained in the nib/storyboard? This can be found under the 'Identity Inspector'. – ThisDarkTao May 28 '12 at 14:16
1

If you use the Storyboard in your project, you have to set the UIViewController's Class field to be your "FriendsController" on Identity Inspector of your storyboard. So you can present your UIVIewController to be using your correct class (In this case, FriendController for you).

Pei.

Pei
  • 11,452
  • 5
  • 41
  • 45
  • Is this initial view controller of the storyboard? And do you use the tableview on this UIViewController? – Pei May 28 '12 at 14:33
  • I've seen your edited question. And I think that it is due to datasource of the tableview that you're using. Please check the datasources of them again and remove unnecessary items from them. – Pei May 28 '12 at 14:40