我正在尝试为 iPhone 创建一个应用程序,在其中从数据库中提取数据,然后将其显示在表格上(主屏幕上的一个全新视图,我要求用户在其中输入数据)。 我使用这个框架来帮助在我的应用程序中切换视图: http://www.pushplay.net/2009/05/framework-for-having-multiple-views-in-an-iphone-app/
基本上,我稍微修改了它。 我已经很好地实现了它,但是当我填充表时,我必须从类中传递信息数组,在该类中我将数据提取到显示表的视图中。 我发现我的代码存在问题(如下所示)。
-(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]]
//============结果页.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 文件
#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
[[编辑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" 包:[NSBundle mainBundle]];
在表视图数据委托方法中:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
所以可能返回的值是错误的或者有些参数。我们需要 ResultsPage 代码。
再见
我发现了我的问题。 就是这个条款:
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 的人不会轻易找到它
错误消息告诉您,您正在对不是 UITableViewDataSource 的对象调用 UITableViewDataSource 方法,因为它没有实现该方法:
tableView:numberOfRowsInSection
。
此方法与 UITableView 对象有关,并且我在代码中的任何位置(以及 PushPlay 示例中)都没有看到任何 UITableView 对象。
如果没有仔细梳理的话,我想说你正在尝试使用 NSMutableArray 来代替 UITableView 及其委托。数组为 UITableView 提供了很好的数据源,但不能直接替代 tableView。