为什么我会收到错误信息"[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x4e38c50'"?

5
我创建了一个 TabBarApplication,并创建了3个控制器,其中一个继承自UITableViewController。我将该控制器的 section 数量设置为“return 1”,每个 section 中的行数设置为“return 2”。但是我遇到了以下问题,为什么会这样呢?
错误提示:[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x4e38c50'
#import <UIKit/UIKit.h>


@interface List : UITableViewController {

}

@end

and .m file is

    #import "List.h"


    @implementation List


    #pragma mark -
    #pragma mark View lifecycle

/*
- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
*/

/*
- (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];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


#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;
}


// 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...

    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
    /*
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
    // ...
    // Pass the selected object to the new view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
    */
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

请检查.h(头文件)中是否有@interface yourViewControllerName:UITableViewController?请确认一下。另外,请发布您的代码。 - Parth Bhatt
是的,头文件是正确的,我正在检查UITableViewController子类,并创建xib,在创建新文件时。 - Chatar Veer Suthar
这里没有任何代码,只是创建新文件,检查UITableViewController子类并制作xib,然后填充各个部分和行。 - Chatar Veer Suthar
4个回答

12

请检查tableView的数据源,我认为它没有正确设置。如果数据源未正确设置,则可能会导致问题。


2
当你设置一个未实现delegate/datasource protocol中声明的方法的表视图delegate/datasource时,就会发生这种情况。
如果你使用xcode模板创建控制器,请确保将其初始化为UITableViewController而不是一般的UIVIewController
如果你发布你的代码,我可以帮你找出问题所在,但是正如我所说,当tableview delegate未实现tableView:numberOfRowsInSection:方法时,就会出现这个错误;如果你说你正在实现这个方法,那么tableview没有分配链接到该控制器类。

1

我经常犯这个错误。在Outlets下,确保将outlet拖到“ViewController”而不是“View”。我通常使用“Document Outline”来连接以确保。希望这可以帮到你。


0

我遇到了同样的错误。我通过将我的视图控制器类从UIViewController更改为ViewController(您的视图控制器类ViewController.h)来解决它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接