2010-08-28 221 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]; 
相關問題