i've a little problem with my first table view, i'm using an example taken from internet but when i run the code, it crash saying:
2011-09-15 15:58:48.873 fptNew[1710:207] -[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x4b3d120
2011-09-15 15:58:48.876 fptNew[1710:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x4b3d120'
here is the code:
// familySelect.m
// fptNew
//
// Created by Marco on 14/09/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
#import "familySelect.h"
@implementation familySelect
@synthesize colorNames;
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"eseguito");
self.colorNames = [[NSArray alloc] initWithObjects:@"Red", @"Green", @"Blue", @"Indigo", @"Violet", nil];
//UILabel *label = (UILabel *)[self.view viewWithTag:111];
//label.backgroundColor = [UIColor colorWithRed:0.2f green:0.3f blue:0.4f alpha:0.00000f];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.colorNames count];
}
// Customize the appearance of table view cells.
- (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] autorelease];
}
// Configure the cell.
cell.textLabel.text = [self.colorNames objectAtIndex: [indexPath row]];
return cell;
}
- (void)dealloc {
[super dealloc];
}
@end
here is the .h file:
// familySelect.h
// fptNew
//
// Created by Marco on 14/09/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
#import <UIKit/UIKit.h>
@interface familySelect : UIViewController
<UITableViewDelegate, UITableViewDataSource> {
NSArray *colorNames;
}
@property (nonatomic, retain) NSArray *colorNames;
@end
and in the .xib file i've set the table view dataSource and delegato to the File's Owner (familySelect)
don't understand where is the problem, thanks开发者_JAVA技巧 for any help
You will need to make the following connections within Interface Builder
- table to File's Owner (select delegate)
- table to File's Owner (select datasource)
- File's Owner to view (select view) : this connection is made automatically
- File's Owner to table (select tableView)
See also this page
Where is your IBOutlet UITableView* myTableView ?
It seems you have passed the Tableview's method on the View Controller and hence crashing.
-[UIViewController tableView:numberOfRowsInSection:]
You have connected the View for the VIew Controller to the TableView. Instead, declare a UITableView and connect this tableview over there.
精彩评论