嗨,我正在开发一个应用程序,在该应用程序中单个窗口中将有多个表,代码运行良好,构建成功,但是当我在Stimulator中运行代码时,它显示错误为:Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x6a71730'

请找到我的代码,并提供一些建议以解决它。

代码遵循

#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


表一:

#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


表2和表3的类似代码。

最佳答案

第一:
您的表是UITableViewCell子类,应为UITableView

第二:
我假设视图控制器是tableviews的委托。
您是否将表视图委托和数据源链接到NIB中的视图控制器?

视图控制器必须提供表的数据。
这就是例外的意思:
-无法识别的选择器。 (tableView:numberOfRowsInSection:

此方法是UITableViewDatasource的一部分。您必须实现不同的数据源方法。
您可以通过使控制器符合协议来查看哪些方法:

@inteface ViewController : UIViewController <UITableViewDelegate, UITableViewDatasource>
....
@end


您应该查看UITableView参考here

关于iphone - 单个窗口中的多个表 View ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8742102/

10-11 20:04