单个窗口中的多个表视图

时间:2012-01-05 11:57:37

标签: iphone ios ipad

您正在开发应用程序,其中单个窗口中将有多个表,代码正常构建成功,但当我在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的类似代码。

2 个答案:

答案 0 :(得分:0)

<强>首先
您的表是子类UITableViewCell,应该是UITableView

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

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

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

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

您应该查看UITableView参考here

答案 1 :(得分:0)

首先检查wether loadView是否在tableOne类中执行。 并注意到tableOne是UITableViewCell的子类,它应该是UITableView吧..?