Hi i am developing app in which there will be multiple tables in single window, Code goes fine Build got Succeeded, But when i run the code in Stimulator it shows the error as:Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x6a71730'
Please find my code and provide me siome suggestions to solve it.
Code Follows
#import <UIKit/UIKit.h>
@class ViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;
@end
@implementation AppDelegate
@synthesize window = _window;
@synthesize viewController = _viewController;
- (void)dealloc
{
[_window release];
[_viewController release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
@end
ViewController:
#import <UIKit/UIKit.h>
#import "tabelOne.h"
#import "tabelTwo.h"
#import "tabelThree.h"
@interface ViewController : UIViewController{
tabelOne *tabel1;
tabelTwo *tabel2;
tabelThree *tabel3;
UITableView *myTableView;
UITableView *myTableView1;
UITableView *myTableView2;
}
@property (retain, nonatomic) IBOutlet UITableView *myTableView;
@property (retain, nonatomic) IBOutlet UITableView *myTableView1;
@property (retain, nonatomic) IBOutlet UITableView *myTableView2;
@end
#import "ViewController.h"
@implementation ViewController
@synthesize myTableView;
@synthesize myTableView1;
@synthesize myTableView2;
- (void)viewDidLoad {
[super viewDidLoad];
if (tabel1 == nil) {
tabel1 = [[tabelOne alloc] init];
}
if (tabel2 == nil) {
tabel2 = [[tabelTwo alloc] init];
}
if (tabel3 == nil) {
tabel3 = [[tabelThree alloc] init];
}
[myTableView setDataSource:tabel1];
[myTableView1 setDataSource:tabel2];
[myTableView2 setDataSource:tabel3];
[myTableView setDelegate:tabel1];
[myTableView1 setDelegate:tabel2];
[myTableView2 setDelegate:tabel3];
}
- (void)viewDidUnload
{
[self setMyTableView:nil];
[self setMyTableView1:nil];
[self setMyTableView2:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
- (void)dealloc {
[myTableView release];
[myTableView1 release];
[myTableView2 release];
[super dealloc];
}
@end
Table one:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
@interface tabelOne : UITableViewCell <UITableViewDataSource, UITableViewDelegate>{
NSMutableArray *items;
}
@end
#import "tabelOne.h"
@implementation tabelOne
-(void)loadView
{
if (items == nil) {
items = [[NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"6",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",nil] retain];
}
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
return [items count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyIdentifier"];
}
cell.textLabel.text = [NSString stringWithFormat:@"1.%@" ,[items objectAtIndex:indexPath.row]];
return cell;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
if(editingStyle == UITableViewCellEditingStyleDelete) {
//Delete the object from the table.
[items removeObjectAtIndex:indexPath.row];
[tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
-(void) dealloc
{
[items release];
[super dealloc];
}
@end
and Similar code for table 2 and 3.
First:
Your Table is of a subclass
UITableViewCell, this should beUITableView.Second:
I assume the view controller is the delegate for the tableviews.
Did you link the tableviews delegates and datasources to the view controller in the NIB?
The view controller must provide the data for tables.
THis is what the exception is saying:
– Unrecognized selector. (
tableView:numberOfRowsInSection:)This method is part of the UITableViewDatasource. You must implement different datasource methods.
You can see which methods by making the controller conform to the protocol as so:
You should look at the UITableView reference here