iOS - 无法加载表格视图

时间:2011-09-15 15:56:44

标签: iphone ios uitableview

我有一个简单的表视图有点问题,我无法显示它,当我打开视图时应用程序崩溃,这里是代码:

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

,这是崩溃时的控制台输出:

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                    0x0171e1c4 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'

不明白我的代码有什么问题

感谢您的帮助:))

更新

我在xib文件中有连接。

tableView连接

enter image description here

和文件所有者连接

enter image description here

是正确的链接方式吗?

2 个答案:

答案 0 :(得分:2)

您必须为表视图声明一个属性。像这样:

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

并且您的界面声明应如下所示:

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

干杯!

答案 1 :(得分:0)

您需要实施UITableView委托 -

@interface familySelect : UIViewController <UITableViewDelegate,UITableViewDataSource>

此外,由于您已将tableView声明为IBOutlet,我猜您已在笔尖中创建了一个tableView。您还必须在笔尖中为tableView创建一个引用插座。只需右键单击{{1}},然后使用正确的元素连接不同的属性。