2011-09-15 25 views
0

我已經有點問題,我的第一個表視圖,我用我從網上取一個例子,但是當我運行的代碼,它崩潰說:的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)

不明白問題出在哪裏,感謝您的幫助

+1

文件的所有者是您的「familySelect」類型嗎?我只問,因爲它在錯誤中說UIViewController不支持該選擇器,這是真的,但是你的「familySelect」類型應該。 – Sam

+0

你是否連接數據源和表的代表文件的所有者? – Nekto

+0

是的兩個問題 –

回答

3

你需要做界面生成器中的以下連接

  • 表格文件的所有者(選擇代表)
  • 表格文件的所有者(選擇數據源)
  • 文件所有者查看(選擇視圖):這個連接自動進行
  • 文件的所有者表(選擇的tableView)

also this page

+0

謝謝你,完成但仍然無法正常工作,但錯誤已經改變 –

3

哪裏是你的IBOutlet中的UITableView * myTableView?

+0

添加和連接,謝謝,但仍然不工作,現在的錯誤說: 謝謝你的回答,我用這種方法在.h文件中聲明瞭UITableView: IBOutlet UITableView0 * tableView; 並將文件的所有者視圖和文件所有者表連接到tableView,現在錯誤已經改變並且說: 2011-09-15 17:00:30.242 fptNew [1927:207] ***終止應用程序,由於未捕獲異常'NSUnknownKeyException',原因:'[ setValue:forUndefinedKey:]:這個類不是關鍵值編碼兼容的關鍵tableView'。 –

+0

你有沒有在接口生成器中連接你的tableView? – ARC

+0

這個錯誤出現在InterfaceBuilder出錯的地方.. goto ViewController ..右鍵點擊它..用黃色驚歎號刪除 –

2

看來你已經通過TableView的方法在視圖控制器上,因此崩潰。

-[UIViewController tableView:numberOfRowsInSection:] 

您已將VIew控制器的視圖連接到TableView。相反,聲明一個UITableView並在那裏連接這個tableview。

+0

謝謝你的回答,我已經聲明瞭一個UITableView。h文件: IBOutlet UITableView0 * tableView; 並將文件的所有者視圖和文件所有者表連接到tableView,現在錯誤已經改變並且說: 2011-09-15 17:00:30.242 fptNew [1927:207] ***終止應用程序,由於未捕獲異常'NSUnknownKeyException',原因:'[ setValue:forUndefinedKey:]:這個類不是關鍵值編碼兼容的關鍵tableView'。 –

+0

您是否將ViewController的視圖連接到Xib文件中的UIVIew? – mayuur