Ios NSInvalidArgumentException tableView:numberOfRowsInSection错误…但我没有使用选项卡式视图,也没有使用Interface Builder
我正在尝试为iPhone创建一个应用程序,从数据库中提取数据,然后将其显示在一个表格上,在主屏幕上显示一个全新的视图,我要求用户输入数据。我使用此框架来帮助切换应用程序中的视图: 基本上,我对它做了一点修改。我已经很好地实现了它,但是当我填充表时,我必须将一个类中的信息数组传递到我显示表的视图中,我从该类中提取数据。我发现下面显示的代码有问题Ios NSInvalidArgumentException tableView:numberOfRowsInSection错误…但我没有使用选项卡式视图,也没有使用Interface Builder,ios,objective-c,iphone,Ios,Objective C,Iphone,我正在尝试为iPhone创建一个应用程序,从数据库中提取数据,然后将其显示在一个表格上,在主屏幕上显示一个全新的视图,我要求用户输入数据。我使用此框架来帮助切换应用程序中的视图: 基本上,我对它做了一点修改。我已经很好地实现了它,但是当我填充表时,我必须将一个类中的信息数组传递到我显示表的视图中,我从该类中提取数据。我发现下面显示的代码有问题 -(void) displayView:(int)intNewView{ NSLog(@"%i", intNewView); [curr
-(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];
}
该表可以正常传递,但当我尝试显示视图时,由于未捕获的异常“NSInvalidArgumentException”,导致终止应用程序时出现此错误,原因:“-[ResultsPage tableView:numberOfRowsInSection::无法识别的选择器发送到实例0x4e396c0。我觉得我没有提供足够的信息,所以如果您想了解更多信息,请不要犹豫。我不确定还有什么含糊不清的地方,所以让我知道如何更具体一些会有帮助。谢谢大家
[[编辑1]]
//=============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
[[编辑2]]
//==TableOfItems.h文件
#import <UIKit/UIKit.h>
@interface TableOfItems : UITableViewController {
NSMutableArray *listOfItems;
NSMutableArray *passedThroughTable;
}
@property (nonatomic, retain) NSMutableArray* passedThroughTable;
@end
//====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
我认为问题不在显示给您的代码中。读取异常时,问题出在ResultsPage类中 ResultsPage*rP=[[ResultsPage alloc]initWithNibName:@TableOfItems bundle:[NSBundle mainBundle]] 在tableview数据委托方法中:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
因此,可能返回的值是错误的或是一些参数。我们需要结果页面代码
再见错误消息告诉您正在对非UITableViewDataSource的对象调用UITableViewDataSource方法,因为它没有实现方法:tableView:numberOfRowsInSection。 此方法与UITableView对象有关,我在代码或PushPlay示例中都没有看到任何UITableView对象。 不用细细梳理,我会说您正在尝试使用NSMutableArray来代替UITableView及其代理。 数组是UITableView的良好数据源,但不能直接替代tableView 希望有帮助
-迈克我发现了我的问题。这是一条:
ResultsPage *rP = [[ResultsPage alloc] initWithNibName:@"TableOfItems" bundle:[NSBundle mainBundle]];
TableOfItems *tOI = [[TableOfItems alloc] init];
应该是
ResultsPage *rP = [[ResultsPage alloc] init];
TableOfItems *tOI = [[TableOfItems alloc] initWithNibName:@"TableOfItems" bundle:[NSBundle mainBundle]];
我有一个名为TableOfItems的.xib文件,它应该与填充用户界面的类TableOfItems相对应。这个类包含所有创建行、节等的委托
很抱歉,我犯了这样一个愚蠢的错误…我认为没有访问.xcodeproj的权限的人不容易发现这一点什么类继承resultsTable?我显示resultsTable属于TableOfItems类型,我只是为此添加了代码…我不确定是否回答了您的问题。
ResultsPage *rP = [[ResultsPage alloc] init];
TableOfItems *tOI = [[TableOfItems alloc] initWithNibName:@"TableOfItems" bundle:[NSBundle mainBundle]];