【问题标题】:UIViewController with UITableView crashingUIViewController 与 UITableView 崩溃
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

  • 另外,我想知道您是否应该使用 self.dataForMyTable2 = 来确保保留它。在你的 numberOfRowsInSection 中:添加 NSLog(@"row count is %d",[dataForMyTable2 count]);
  • 它在哪里崩溃 - 哪一行?一旦它崩溃,在调试器中输入“bt” - 在(GDB)提示符下。这将生成一个回溯并告诉你它在哪里崩溃。请张贴。
  • 这是您的建议的新回溯link
  • 好吧,你有一个僵尸表(真的!)。 objc_msgSend () 表示您正在向已释放(僵尸)实例发送消息。 (搜索 NSZombieEnabled 以了解如何打开它。)这意味着您的 myDataTable 尚不可用。它在您的“reloadData”调用中崩溃。因此,请尝试将此 reloadData 移动到 viewDidAppear,或完全不使用它 - 表格应在首次显示时自行加载。
  • 我删除了 reloadData,但没有任何变化。这是 NSZombie 日志 '2011-05-05 22:14:15.486 Tableviewtest[4911:207] *** -[uWDataResumeController numberOfSectionsInTableView:]: message sent to deallocated instance 0x4e306e0' same stacktrace
【解决方案2】:

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

我以为可能是 tableview 被释放了两次,但我无法复制崩溃。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2012-04-03
    • 1970-01-01
    • 1970-01-01
    • 2018-07-15
    • 1970-01-01
    相关资源
    最近更新 更多