简体   繁体   English

NSInvalidArgumentException tableView:numberOfRowsInSection错误…但是我没有使用选项卡式视图,也没有使用过Interface Builder

[英]NSInvalidArgumentException tableView:numberOfRowsInSection error…but I am not using a tabbed view and I haven't used the Interface Builder at all

I'm trying to create an app for the iPhone where I pull data from a database and then display it on a table (a completely new view from the main screen in which I ask the user to enter in data). 我正在尝试为iPhone创建一个应用程序,在该应用程序中,我从数据库中提取数据,然后将其显示在表上(从主屏幕中请求用户输入数据的主屏幕上的全新视图)。 I've used this framework to help switch views in my app: http://www.pushplay.net/2009/05/framework-for-having-multiple-views-in-an-iphone-app/ 我使用此框架来帮助切换应用程序中的视图: http : //www.pushplay.net/2009/05/framework-for-having-multiple-views-in-an-iphone-app/

And basically, I modified it a little bit. 基本上,我做了一点修改。 I have it implemented fine, but when I populate the table I have to pass the array of information from a class where I pull the data from into the view where I display the table. 我已经实现了很好的功能,但是当我填充表时,我必须传递来自类的信息数组,在该类中,我将数据从中拉到显示表的视图中。 I found the problem with my code (displayed below). 我发现我的代码有问题(如下所示)。

-(void) displayView:(int)intNewView{
    NSLog(@"%i", intNewView);
    [currentView.view removeFromSuperview];
    [currentView release];

    ServiceProvider *g = [[ServiceProvider alloc] init];
    ServiceProvider *l = [[ServiceProvider alloc] init];

    [g setSPNAME:@"george"];
    [l setSPNAME:@"luuuuuusaaaa"];

    passInTableToTOI = [[NSMutableArray alloc] initWithObjects:g, l, nil];

    ResultsPage *rP = [[ResultsPage alloc] initWithNibName:@"TableOfItems" bundle:[NSBundle mainBundle]];
    TableOfItems *tOI = [[TableOfItems alloc] init];

    switch (intNewView) {
        case 1:
            currentView = [[SearchPage alloc] init];
            break;
        case 2:
            [tOI setPassedThroughTable:passInTableToTOI];
            [rP setResultsTable:tOI];
            currentView = rP;
            break;
        case 3:
            currentView = [[ShowAllPage alloc] init];
            break;
        /*default:
            break;*/
    }

    //[rP release];
    //[tOI release];
    [self.view addSubview:currentView.view];
}

The table gets passed fine, but when I try to display the view I get this error "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ResultsPage tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x4e396c0". 该表已通过,但当我尝试显示视图时,出现此错误“由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[ResultsPage tableView:numberOfRowsInSection:]:无法识别的选择器已发送到实例0x4e396c0”。 I feel like I'm not giving enough information, so please if you'd like more information don't hesitate. 我觉得我没有提供足够的信息,所以如果您想了解更多信息,请不要犹豫。 I'm not sure what else I'm being vague on so it'd help to let me know how I can be more specific. 我不确定我还有什么模糊的地方,所以让我知道我可以变得更具体会有所帮助。 Thanks a lot everyone. 非常感谢大家。

[[EDIT 1]] [[编辑1]]

//===========ResultsPage.h //===========ResultsPage.h

#import <UIKit/UIKit.h>

@class TopBottomRectangles;
@class TableOfItems;
@class SearchTextBox;

@interface ResultsPage : UIViewController {

    TopBottomRectangles *tbRects;
    TableOfItems *resultsTable;
    SearchTextBox *sTB;

}

@property (nonatomic, retain) IBOutlet TableOfItems *resultsTable;

@end

//===================ResultsPage.m file //===================ResultsPage.m文件

#import "ResultsPage.h"
#import "TopBottomRectangles.h"
#import "TableOfItems.h"
#import "SearchTextBox.h"

#import "MultiviewAppDelegate.h"


@implementation ResultsPage
@synthesize resultsTable;

-(void)goToShowAllPage
{
    MultiviewAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    [appDelegate displayView:3];
}

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

    NSLog(@"load the results page");

    tbRects = [[TopBottomRectangles alloc] init];
    sTB = [[SearchTextBox alloc] init];

[self.view addSubview:[resultsTable view]];
[self.view addSubview:tbRects.bottomBG_View];
[self.view addSubview:tbRects.topBG_View];
[self.view addSubview:sTB.textBox_BG_border];
[self.view addSubview:sTB.textBox_BG];
[self.view addSubview:sTB.textBox];

//[self.view addSubview:btnTwo];

[super viewDidLoad];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

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


- (void)dealloc {
    NSLog(@"dealloc results page");
    [sTB release];
    [resultsTable release];
    [tbRects release];
    [super dealloc];
}


@end

[[EDIT 2]] [[编辑2]]

//====TableOfItems.h file //====TableOfItems.h文件

#import <UIKit/UIKit.h>
@interface TableOfItems : UITableViewController {
    NSMutableArray *listOfItems;
    NSMutableArray *passedThroughTable;
}

@property (nonatomic, retain) NSMutableArray* passedThroughTable;

@end

//=====TableOfItems.m file //=====TableOfItems.m文件

#import "TableOfItems.h"
#import "MyTableCell.h"
#import "ServiceProvider.h"


@implementation TableOfItems
@synthesize passedThroughTable;

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

    self.navigationController.navigationBarHidden = YES;
    //set the size of the table
    [self fixTableSize];

    NSMutableArray *spName = [[NSMutableArray alloc] init];
    for (int i = 0; i < [passedThroughTable count]; i++){

        ServiceProvider *willBeGone = [[ServiceProvider alloc] init];
        willBeGone = [passedThroughTable objectAtIndex:i];
        [spName addObject:willBeGone.SPNAME];

        NSLog(@"%@", [spName objectAtIndex:i]);
        //[willBeGone release];

    }

    //Initialize the array.
    listOfItems = [[NSMutableArray alloc] init];

    NSArray *countriesToLiveInArray = [NSArray arrayWithObjects:@"Iceland", @"Greenland", @"Switzerland", @"Norway", @"New Zealand", @"Greece", @"Rome", @"Ireland", nil];
    NSDictionary *countriesToLiveInDict = [NSDictionary dictionaryWithObject:countriesToLiveInArray forKey:@"Countries"];

    NSArray *countriesLivedInArray = [NSArray arrayWithObjects:@"India", @"U.S.A", nil];
    NSDictionary *countriesLivedInDict = [NSDictionary dictionaryWithObject:countriesLivedInArray forKey:@"Countries"];

    [listOfItems addObject:countriesToLiveInDict];
    [listOfItems addObject:countriesLivedInDict];
}

-(void)fixTableSize{

    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    CGFloat screenScale = [[UIScreen mainScreen] scale];

    NSInteger height = screenBounds.size.height * screenScale;
    NSInteger windowHeight = 100;
    NSInteger yFromTop = 60;
    NSInteger yFromBottom = height + 30 - windowHeight;

    self.tableView.frame = CGRectMake(0,yFromTop,320,yFromBottom - yFromTop);

}



#pragma mark -
#pragma mark Table view data source

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

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    NSArray *sectionTitles = [[NSArray alloc] initWithObjects:@"Countries to visit", @"Countries visited", nil];

    return [sectionTitles objectAtIndex:section];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

    NSLog(@"====");

    //Number of rows it should expect should be based on the section
    NSDictionary *dictionary = [listOfItems objectAtIndex:section];
    NSArray *array = [dictionary objectForKey:@"Countries"];
    return [array count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *MyIdentifier = [NSString stringWithFormat:@"MyIdentifier %i", indexPath.row];
    MyTableCell *cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
    NSArray *array = [dictionary objectForKey:@"Countries"];

    if (cell == nil) {
        cell = [[[MyTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];

        UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(1, 1, 30.0, 
                                                                tableView.rowHeight)] autorelease]; 
        [cell addColumn:60];
        //label.tag = FIRCOL_TAG; 
        label.font = [UIFont systemFontOfSize:12.0]; 
        label.text = [NSString stringWithFormat:@"%d:00", indexPath.row];
        label.textAlignment = UITextAlignmentRight; 
        label.textColor = [UIColor blueColor]; 
        label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | 
        UIViewAutoresizingFlexibleTopMargin; 
        [cell.contentView addSubview:label]; 

        label =  [[[UILabel alloc] initWithFrame:CGRectMake(70.0, 0, 110, 
                                                        tableView.rowHeight)] autorelease]; 
        [cell addColumn:260];
        //label.tag = SECCOL_TAG; 
        label.font = [UIFont systemFontOfSize:12.0]; 
        label.text = [NSString stringWithFormat:@"%@", [array objectAtIndex:indexPath.row]];
        label.textAlignment = UITextAlignmentLeft; 
        label.textColor = [UIColor blackColor]; 
        label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | 
        UIViewAutoresizingFlexibleHeight; 
        [cell.contentView addSubview:label];

        label =  [[[UILabel alloc] initWithFrame:CGRectMake(270.0, 0, 30, 
                                                        tableView.rowHeight)] autorelease]; 
        //[cell addColumn:180];
        //label.tag = THIRCOL_TAG; 
        label.font = [UIFont systemFontOfSize:12.0]; 
        // add some silly value
        label.text = [NSString stringWithFormat:@"$%d", indexPath.row * 4];
        label.textAlignment = UITextAlignmentLeft; 
        label.textColor = [UIColor blueColor]; 
        label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | 
        UIViewAutoresizingFlexibleHeight; 
        [cell.contentView addSubview:label];
    }

    return cell;

} 





@end

I think the problem is not in the code that shows you. 我认为问题不在显示您的代码中。 When you read the Exception, the problem is in ResultsPage class 当您读取异常时,问题出在ResultsPage类中

ResultsPage *rP = [[ResultsPage alloc] initWithNibName:@"TableOfItems" bundle:[NSBundle mainBundle]]; ResultsPage * rP = [[ResultsPage alloc] initWithNibName:@“ TableOfItems”捆绑包:[NSBundle mainBundle]];

in tableview data delegate method : 在的tableview数据委托方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

So maybe the returned value is wrong or some arguments. 因此,也许返回的值是错误的或一些参数。 We need ResultsPage code. 我们需要ResultsPage代码。

Bye 再见

The error message is telling you that you're calling a UITableViewDataSource method on something that is not a UITableViewDataSource, because it does not implement the method: tableView:numberOfRowsInSection. 该错误消息告诉您,您正在不是UITableViewDataSource的对象上调用UITableViewDataSource方法,因为它未实现以下方法:tableView:numberOfRowsInSection。 This method has to do with UITableView objects, and I don't see any UITableView objects anywhere in your code (nor in the PushPlay examples.) Without going through it with a fine-toothed comb, I'd say you're trying to use an NSMutableArray in place of a UITableView and it's delegates. 此方法与UITableView对象有关,并且我在代码的任何地方(在PushPlay示例中也没有看到)没有UITableView对象。如果不使用细齿梳进行检查,我会说您正在尝试使用NSMutableArray代替UITableView及其委托。 An array makes a fine data source for a UITableView, but is not a direct substitute for a tableView. 数组可以为UITableView提供良好的数据源,但不能直接替代tableView。

Hope that helps. 希望能有所帮助。

-Mike -麦克风

I found my problem. 我发现了问题。 It was this clause: 就是这个子句:

    ResultsPage *rP = [[ResultsPage alloc] initWithNibName:@"TableOfItems" bundle:[NSBundle mainBundle]];
    TableOfItems *tOI = [[TableOfItems alloc] init];

It's supposed to be 应该是

    ResultsPage *rP = [[ResultsPage alloc] init];
    TableOfItems *tOI = [[TableOfItems alloc] initWithNibName:@"TableOfItems" bundle:[NSBundle mainBundle]];

I had a .xib file called "TableOfItems" and that should be corresponding to the class that populates the user interface, TableOfItems. 我有一个名为“ TableOfItems”的.xib文件,该文件应对应于填充用户界面TableOfItems的类。 This class hass all those delegates which creates the rows and sections and whatnot. 此类具有所有创建行和节以及所有内容的委托。

I'm sorry that I made such a foolish error...I don't think this would have been easily found by someone who didn't have access to the .xcodeproj 对不起,我犯了一个如此愚蠢的错误...我不认为无法访问.xcodeproj的人不会轻易发现此错误

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用界面构建器在tableView中添加视图 - how can i add a view in tableView using interface builder 使用UITableView时,ios-tableView:numberOfRowsInSection…NSInvalidArgumentException - ios - tableView:numberOfRowsInSection …NSInvalidArgumentException when working with UITableView 使用tableView numberOfRowsInSection时遇到问题 - Trouble using tableView numberOfRowsInSection Swift:Tableview numberOfRowsInSection没有返回我要求的indexpath.row - Swift : Tableview numberOfRowsInSection didn't return the indexpath.row I asked for &#39;NSInvalidArgumentException&#39;-[UIViewController tableView:numberOfRowsInSection:]-无法识别的选择器已发送到实例 - 'NSInvalidArgumentException' - [UIViewController tableView:numberOfRowsInSection:] - unrecognized selector sent to instance 搜索时发生tableView:numberOfRowsInSection错误 - tableView:numberOfRowsInSection error when searching 如何使用NSArrays的NSDictionary设置TableView的titleForHeaderInSection和numberOfRowsInSection? - How can I set a TableView's titleForHeaderInSection and numberOfRowsInSection with an NSDictionary of NSArrays? 无法在Interface Builder中更改TableView的大小 - Can't change the size of a TableView in Interface Builder 我没有使用全部故事板,但我仍然收到此错误 - I am not using storyboard AT ALL but I am still getting this error 使用Interface Builder在选项卡式应用程序的多个视图中放置UIGestureRecognizer - Using Interface Builder to place UIGestureRecognizer in several views in Tabbed application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM