2012-04-18 55 views
4

我想在一個利用ARC的項目中創建一個簡單的UItableview應用程序。表格呈現得很好,但如果我嘗試滾動或點擊一個單元格,應用程序崩潰。滾動或點擊UITableView崩潰 - 問題與ARC?

望着NSZombies(是正確的方法說?)我得到的消息「 - [PlacesViewController respondsToSelector:]:消息發送到釋放實例0x7c29240」

我相信這事做與ARC,因爲我過去成功實現了UItableviews,但這是我第一個使用ARC的項目。我知道我必須錯過一些非常簡單的事情。

PlacesTableViewController.h

@interface PlacesViewController : UIViewController 
<UITableViewDelegate,UITableViewDataSource> 

@property (nonatomic, strong) UITableView *myTableView; 

@end 

PlacesTableViewController.m

#import "PlacesTableViewController.h" 

@implementation PlacesViewController 

@synthesize myTableView; 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; 

    self.myTableView.dataSource = self; 
    self.myTableView.delegate = self; 

    [self.view addSubview:self.myTableView]; 
} 
#pragma mark - UIViewTable DataSource methods 

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

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



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *result = nil; 

    static NSString *CellIdentifier = @"MyTableViewCellId"; 

    result = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if(result == nil) 
    { 
     result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    result.textLabel.text = [NSString stringWithFormat:@"Cell %ld",(long)indexPath.row]; 


    return result; 
} 
@end 
+0

我們可以獲取用於alloc,init和存儲PlacesViewController的代碼嗎? – hukir 2012-10-16 19:02:30

回答

1

沒有什麼明顯的錯誤你發佈的代碼。問題在於創建並保存到PlacesViewController的代碼。您可能正在創建它,但不會將其永久保存在任何地方。您的PlacesViewController需要保存到ivar或放入視圖容器中,以便爲您進行管理(UINavigationController,UITabController或類似)

+0

是的,我遇到了一些問題。解決方案是照顧保存tableView的UIViewController。只要確保提供一個強大的參考。 – rcmcastro 2014-04-11 00:57:37