【发布时间】:2011-08-19 13:37:38
【问题描述】:
我有一个 UIViewController,里面有一个 UITableView。我以编程方式实现了所有内容,但应用程序崩溃或不显示数组中的数据。 这是我的示例代码:
uWDataResumeController.h
#import <UIKit/UIKit.h>
@interface uWDataResumeController : UIViewController <UITableViewDelegate, UITableViewDataSource>{
NSMutableArray *dataForMyTable2;
UITableView *myDataTable;
}
@property (nonatomic, retain) UITableView *myDataTable;
@property (nonatomic, retain) NSMutableArray *dataForMyTable2;
@end
uWDataResumeController
#import "uWDataResumeController.h"
@implementation uWDataResumeController
@synthesize myDataTable, dataForMyTable2;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)dealloc
{
[dataForMyTable2 release];
[myDataTable release];
[super dealloc];
}
- (void)loadView
{
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
}
- (void)viewDidLoad
{
myDataTable = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
myDataTable.separatorStyle = UITableViewCellSeparatorStyleNone;
myDataTable.rowHeight = 86;
myDataTable.backgroundColor = [UIColor clearColor];
myDataTable.delegate = self;
myDataTable.dataSource = self;
[self.view addSubview:myDataTable];
[myDataTable release];
dataForMyTable2 = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", nil];
[myDataTable reloadData];
[super viewDidLoad];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return [dataForMyTable2 count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [dataForMyTable2 objectAtIndex:indexPath.row];
return cell;
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
提前谢谢你!
编辑:添加堆栈跟踪
好消息是我没有像往常一样得到堆栈跟踪,但如果我输入 gdb backtrace 我得到
#0 0x016a7fd7 in CALayerGetDelegate ()
#1 0x0004c76e in -[UIView(Hierarchy) subviews] ()
#2 0x0004304e in -[UIView(Geometry) hitTest:withEvent:] ()
#3 0x000430f2 in -[UIView(Geometry) hitTest:withEvent:] ()
#4 0x0003c6b6 in +[UIWindow _hitTestToPoint:pathIndex:forEvent:] ()
#5 0x0001c709 in _UIApplicationHandleEvent ()
#6 0x00ffa992 in PurpleEventCallback ()
#7 0x00da2944 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ ()
#8 0x00d02cf7 in __CFRunLoopDoSource1 ()
#9 0x00cfff83 in __CFRunLoopRun ()
#10 0x00cff840 in CFRunLoopRunSpecific ()
#11 0x00cff761 in CFRunLoopRunInMode ()
#12 0x00ff91c4 in GSEventRunModal ()
#13 0x00ff9289 in GSEventRun ()
#14 0x00021c93 in UIApplicationMain ()
#15 0x00002039 in main (argc=1, argv=0xbffff064) at main.m:14
【问题讨论】:
-
你能给我们一些错误细节吗?堆栈跟踪?我的猜测是您正在尝试访问不存在的数组元素。但我需要查看错误才能确认。
-
您应该在您的
- (void)loadView...中为您的初始化视图添加一个自动释放,self.view = XXX 将保留该实例。但这不是您崩溃的原因。请向我们展示堆栈跟踪和控制台输出。 -
在第一篇文章中添加了堆栈跟踪
标签: iphone cocoa-touch uitableview uiviewcontroller crash