iOS - 表格视图错误

时间:2011-09-15 14:10:59

标签: ios uitableview

我的第一个表格视图有点问题,我正在使用从互联网上获取的示例,但是当我运行代码时,它崩溃说:

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文件中我将表格视图dataSource和delegato设置为文件所有者(familySelect)

不明白问题出在哪里,谢谢你的帮助

3 个答案:

答案 0 :(得分:3)

您需要在Interface Builder

中建立以下连接
  • 表格到文件的所有者(选择代理人)
  • 表格到文件的所有者(选择数据源)
  • 要查看的文件所有者(选择视图):此连接是自动进行的
  • 文件的所有者到表(选择tableView)

请参阅also this page

答案 1 :(得分:3)

您的IBOutlet UITableView * myTableView在哪里?

答案 2 :(得分:2)

您似乎已经在View Controller上传递了Tableview的方法,因此崩溃了。

 -[UIViewController tableView:numberOfRowsInSection:]

您已将VIew控制器的视图连接到TableView。相反,声明一个UITableView并在那里连接这个tableview。