iphone - 带有UITableView的UIViewController崩溃

标签 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);
}

先感谢您!

编辑:添加了堆栈跟踪

令人高兴的是,我没有像往常一样获得stacktrace,但是如果我输入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

最佳答案

我认为您应该使用一个临时变量:
代替:

    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];

关于iphone - 带有UITableView的UIViewController崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5901304/

相关文章:

ios - 键盘在出现时隐藏 UITextView

ios - cocoa : Can't remove child view controller

ios - 为什么当我滑动 View 时 UITableViewCell 改变了大小

ios - OCMock - 部分模拟 [UIAlertView alloc]

iphone - 在 xcode 中安装 OpenCV

objective-c - 使用UITableView的音频播放器

ios - 分段控件和 tableView

ios - 从 UITableView 选择行时将核心数据对象传递给另一个 View Controller

iphone - 创建一个包含许多产品的数据库

iPhone SDK : How do you download video files to the Document Directory and then play them?