开发者

iOS - Unable to load a table view

i've a little problem with a simple table view, i'm unable to display it, the app crash when i open the view, here is the code:

familySelect.h:

//  familySelect.h
//  fptNew
//
//  Created by Marco on 14/09/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.

#import <UIKit/UIKit.h>


@interface familySelect : UIViewController {
    NSArray *colorNames;
    IBOutlet UITableView *tableView;
}

@property (nonatomic, retain) NSArray *colorNames;

@end

familySelect.m:

//  familySelect.m
//  fptNew
//
//  Created by Marco on 14/09/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.

#import "familySelect.h"


@implementation familySelect

@synthesize colorNames;

 // 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 {
    [super viewDidLoad];

    NSLog(@"eseguito");
    self.colorNames = [[NSArray alloc] initWithObjects:@"Red", @"Green", @"Blue", @"Indigo", @"Violet", nil];

    //UILabel *label = (UILabel *)[self.view viewWithTag:111];
    //label.backgroundColor =   [UIColor colorWithRed:0.2f green:0.3f blue:0.4f alpha:0.00000f];

}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}


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

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)TableView numberOfRowsInSection:(NSInteger)section {
    return [self.colorNames count];
}

// 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.
    cell.textLabel.text = [self.colorNames objectAtIndex: [indexPath row]];

    return cell;
}

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

@end

and that is the console output when it crash:

2011-09-15 17:46:02.788 fptNew[2098:207] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x4e29540> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key tableView.'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x00eab5a9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x00cda313 objc_exception_throw + 44
    2   CoreFoundation                      0x00eab4e1 -[NSException raise] + 17
    3   Foundation                          0x0002f677 _NSSetUsingKeyValueSetter + 135
    4   Foundation                          0x0002f5e5 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 285
    5   UIKit                               0x004b330c -[UIRuntimeOutletConnection connect] + 112
    6   CoreFoundation                      0x00e218cf -[NSArray makeObjectsPerformSelector:] + 239
    7   UIKit                               0x004b1d23 -[UINib instantiateWithOwner:options:] + 1041
    8   UIKit                               0x004b3ab7 -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 168
    9   UIKit                               0x00369628 -[UIViewController _loadViewFromNibNamed:bundle:] + 70
    10  UIKit                               0x00367134 -[UIViewController loadView] + 120
    11  UIKit                               0x0036700e -[UIViewController view] + 56
    12  fptNew                              0x00001e2f -[homeScreen buttonPressed:] + 163
    13  UIKit                               0x002b74fd -[UIApplication sendAction:to:from:forEvent:] + 119
    14  UIKit                               0x00347799 -[UIControl sendAction:to:forEvent:] + 67
    15  UIKit                               0x00349c2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
    16  UIKit                               0x00348a1c -[UIControl touchesBegan:withEvent:] + 277
    17  UIKit                               0x002dbd41 -[UIWindow _sendTouchesForEvent:] + 395
    18  UIKit                               0x002bcc37 -[UIApplication sendEvent:] + 447
    19  UIKit                               0x002c1f2e _UIApplicationHandleEvent + 7576
    20  GraphicsServices                    0x0171f992 PurpleEventCallback + 1550
    21  CoreFoundation                      0x00e8c944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    22  CoreFoundation                      0x00deccf7 __CFRunLoopDoSource1 + 215
    23  CoreFoundation                      0x00de9f83 __CFRunLoopRun + 979
    24  CoreFoundation                      0x00de9840 CFRunLoopRunSpecific + 208
    25  CoreFoundation                      0x00de9761 CFRunLoopRunInMode + 97
    26  GraphicsServices                    0x0171e1开发者_JAVA技巧c4 GSEventRunModal + 217
    27  GraphicsServices                    0x0171e289 GSEventRun + 115
    28  UIKit                               0x002c5c93 UIApplicationMain + 1160
    29  fptNew                              0x00001b48 main + 102
    30  fptNew                              0x00001ad9 start + 53
)
terminate called after throwing an instance of 'NSException'

Don't understand what's wrong with my code

thanks for any help :)

UPDATE:

there are the connection i've don in the xib file.

tableView connections

iOS - Unable to load a table view

and the File's Owner Connections

iOS - Unable to load a table view

is that the correct way to link?


You have to declare a property for the table view. Like this:

@property(nonatomic,retain) IBOutlet UITableView *tableView;

and your interface declaration should look like this:

@interface familySelect : UIViewController<UITableViewDelegate,UITableViewDataSource> 
{
      NSArray *colorNames;
      UITableView *tableView;
}

Cheers!


You need to implement the UITableView delegates-

@interface familySelect : UIViewController <UITableViewDelegate,UITableViewDataSource>

Also, since you've declared the tableView as an IBOutlet, I'm guessing you've made one in the nib. You'll have to make a referencing outlet for tableView in the nib too. Just right click the tableView and connect the different properties with the right elements.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜