开发者

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

开发者 https://www.devze.com 2023-03-04 19:50 出处:网络
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).I\'ve

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

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". 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]]

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

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

//====TableOfItems.h file

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

@property (nonatomic, retain) NSMutableArray* passedThroughTable;

@end

//=====TableOfItems.m file

#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 | 
        UIViewAutoresizingFl开发者_如何学运维exibleTopMargin; 
        [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 *rP = [[ResultsPage alloc] initWithNibName:@"TableOfItems" bundle:[NSBundle mainBundle]];

in tableview data delegate method :

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

So maybe the returned value is wrong or some arguments. We need ResultsPage code.

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. 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. An array makes a fine data source for a UITableView, but is not a direct substitute for a 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. 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

0

精彩评论

暂无评论...
验证码 换一张
取 消