2010-08-28 228 views
1

以下测试代码导致EXC_BAD_ACCESS滚动许多时间。 会有人喜欢告诉我这个问题吗?UINavigationController中的UITableView导致EXC_BAD_ACCESS

-myview.h ------------------------------------------ ---------

@interface MyView : UINavigationController < UITableViewDataSource, UITableViewDelegate, UINavigationBarDelegate > 
{ 
    UITableView* mTableView; 
    NSMutableArray* data; 
} 

-myview.m ------------------------------ ---------------------

@implementation MyView 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    int th = self.navigationBar.frame.size.height; 
    int w = self.view.frame.size.width; 
    int h = self.view.frame.size.height; 

    mTableView = [[[UITableView alloc] initWithFrame: 
       CGRectMake(0.0f, th, w, h - th) style:UITableViewStylePlain] retain]; 
    mTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | 
             UIViewAutoresizingFlexibleHeight; 
    mTableView.dataSource = self; 
    mTableView.delegate = self; 
    [mTableView setRowHeight:55]; 

    [self.view addSubview:mTableView]; 

    data = [[[NSMutableArray alloc] init] retain]; 

    for (int i = 0; i < 150; i++) 
    { 

     [data addObject:[NSDictionary 
     dictionaryWithObjects:[NSArray arrayWithObjects:@"good", [UIColor blackColor], nil] 

     forKeys:[NSArray arrayWithObjects:@"name", @"color", nil]]]; 
    } 
} 

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 150; 
} 

- (UITableViewCell *)tableViewCellWithReuseIdentifier:(NSString *)identifier 
{ 
    UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease]; 
    cell.textLabel.text = @"goooood"; 
    return cell; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 
     cell = [self tableViewCellWithReuseIdentifier:CellIdentifier]; 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
} 


- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
} 

- (void)dealloc 
{ 
    [super dealloc]; 
} 

- (BOOL)canBecomeFirstResponder 
{ 
    return YES; 
} 

@end 
+0

请阅读代码格式的文档。当您发布信息时,右侧有一个关于“标记”的链接。 – 2010-08-28 02:13:42

+0

另外,发生崩溃的帖子,并试图给出相关代码片段 – cobbal 2010-08-28 02:14:40

+0

这与C或C++有什么关系? – 2010-08-28 03:56:15

回答

0

你应该包含您的表视图(它甚至可能是UITableViewController一个子类)视图控制器。我不认为UINavigationController是不会像你所拥有的那样被分类的。如果你想有一个导航控制器,你应该创建CustomTableViewController的一个实例,包含您的tableview,然后使用UINavigationControllerinitWithRootViewController:方法:

CustomTableViewController * ctvc = [[CustomTableViewController alloc] init]; 
UINavigationController * navigationController = [[UINavigationController alloc] initWithRootViewController:ctvc]; 
[ctvc release]; 

// Superview is where you want this added 
// probably your window in app did finish launching 
[superview addSubview:navigationController.view]; 

[navigationController release]; 
相关问题