UITiewController与UITableView崩溃

时间:2011-05-05 16:44:39

标签: iphone cocoa-touch uitableview uiviewcontroller crash

我有一个内置UITableView的UIViewController。我以编程方式实现了所有内容,但应用程序崩溃或不显示数组中的数据。 这是我的示例代码:

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

2 个答案:

答案 0 :(得分:0)

我认为你应该使用临时变量: 而不是:

    myDataTable = [[UITableView alloc] initWithFrame:self.view.bounds      
     style:UITableViewStyleGrouped];

这样做:

UITableView *TEMPDataTable = [[UITableView alloc] initWithFrame:self.view.bounds
  style:UITableViewStyleGrouped];

然后代替:

  [self.view addSubview:myDataTable];
  [myDataTable release];
  dataForMyTable2 = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", nil];
  [myDataTable reloadData];

这样做:

[self.view addSubview:TEMPDataTable];
self.myDataTable=TEMPDataTable;
[TEMPDataTable release];
dataForMyTable2 = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", nil];
[myDataTable reloadData];

答案 1 :(得分:0)

我已将您的代码复制到一个新项目中,它对我来说很正常。

我认为可能是两次发布的tableview,但我无法复制崩溃。