我已經有點問題,我的第一個表視圖,我用我從網上取一個例子,但是當我運行的代碼,它崩潰說:的iOS - 表視圖錯誤
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'
這裏是代碼:
// 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
這裏是.h文件:
// 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
,並在文件中的.xib我已經設置表視圖數據源和delegato到文件的所有者(familySelect)
不明白問題出在哪裏,感謝您的幫助
文件的所有者是您的「familySelect」類型嗎?我只問,因爲它在錯誤中說UIViewController不支持該選擇器,這是真的,但是你的「familySelect」類型應該。 – Sam
你是否連接數據源和表的代表文件的所有者? – Nekto
是的兩個問題 –