UITableView抛出异常

时间:2010-06-04 03:43:18

标签: iphone objective-c cocoa-touch uitableview interface-builder

我在一个非常简单的UITableView上收到以下错误:

 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x5d73210'  

这是我的代码:

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 0;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return 10;
}

2 个答案:

答案 0 :(得分:1)

你必须至少返回1个部分...我已经在一个名为testes的新创建的UIViewController XIB中进行了测试(添加新的.h和.c文件并选中该框以创建XIB并使其成为一个UITableViewController子类),它工作正常。

你是如何创建tableview的?您是否像我上面写的那样创建了自己的XIB,或者您只是将一个tableview放到MainWindow.xib文件中?尝试创造你自己,你应该是好的。只需确保在AppDelegate中将didFinishLaunchingWithOptions上添加的主视图设置为您创建的新tableview。在IB中,tableview的UIViewController应设置为testes子类(这是一个UITableViewController子类)

App委托文件

#import <UIKit/UIKit.h>

@class testes;

@interface testesAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    testes *viewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet testes *viewController;

@end

#import "testesAppDelegate.h"
#import "testes.h"

@implementation testesAppDelegate

@synthesize window;
@synthesize viewController;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after app launch    
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

return YES;
}

然后在viewcontroller .m文件中

#pragma mark Table view methods

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


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

答案 1 :(得分:1)

将0更改为1

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return 10;
}