iphone - iOS - 无法加载表格 View

标签 iphone ios uitableview

我的简单表格 View 有一个小问题,我无法显示它,当我打开 View 时应用程序崩溃,代码如下:

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

这是正确的链接方式吗?

最佳答案

您必须为 TableView 声明一个属性。像这样:

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

您的接口(interface)声明应如下所示:

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

干杯!

关于iphone - iOS - 无法加载表格 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7433842/

相关文章:

ios - 检查 UItextfield 名称

iphone - 如何通过蓝牙将文件发送到其他 iPhone

iphone - 在提高性能的获取请求模板下发生了什么?

ios - *67 为 IOS 应用程序的电话号码

ios - 选择性删除自定义手势识别器

用于 iPhone 的 JavaScript 从非默认 iOS 浏览器在 Safari 中打开

uitableview - 在UITableView或UICollectionView上加载与可见单元格

ios - XCode iPhone 模拟器看起来不像 iPhone

ios - 找不到体系结构 armv7 的符号

swift - 使用 SDWebimage 下载并缓存 MKMapSnapshotter 图像