开发者

UIViewController with UITableView crashing

I've got an UIViewController with a UITableView inside. I implemented everything programmatically but the application crashes or doesn't display the data in the array. Here's my sample code:

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

Thank you in advance!

EDIT: Added stacktrace

The nice thing is that I don't get a stacktrace as usual, but if I type in gdb backtrace i get

#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


I think you should use a temp variable: Instead of:

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

Do this:

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

and then instead of:

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

Do this:

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


i've copied your code into a new project and it works fine for me as is.

I thought it may be the tableview being released twice, but I can't replicate the crash.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜