2010-05-24 38 views
2

我有一個非常簡單的UITableView,它有3個部分,每部分3行。滾動時基本UITableView的內存問題

#pragma mark - 
#pragma mark UITableView delegate methods 

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

- (UITableViewCell *)tableView:(UITableView *)tblView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tblView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Configure the cell... 

    return cell; 

} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tblView 
{ 
    if (tblView == self.tableView) { 
     return 3; 
    } 
    else { 
     return 1; 
    } 
} 

樣樣精顯示出來,但只要我滾動我的應用程序崩潰,我的調試器告訴我:

***** - [ProfileViewController的tableView:的cellForRowAtIndexPath:]:消息發送到釋放實例0x5ae61b0 **

我不完全確定我做錯了什麼。

編輯: 這是怎麼了顯示ProfileViewController:

ProfileViewController* profileView = [[ProfileViewController alloc] initWithNibName:@"ProfileViewController" bundle:nil]; 
    profileView.user_name = username; 
    profileView.message_source = messageSource; 
    [self.navigationController pushViewController:profileView animated:YES]; 
    [profileView release]; 

回答

2

看起來像你的ProfileViewController實例以某種方式獲取釋放。確保在創建後不要調用它的-autorelease

+0

我已更新我的代碼以顯示如何顯示我的ProfileViewController。你看到有什麼不對嗎? – 2010-05-24 18:02:27

+0

我改變了我的NIB包含一個ViewController和一個TableView而不是TableViewController。它似乎在伎倆。不知道爲什麼。 – 2010-05-24 18:20:14

1

您的代碼似乎是正確的。你的bug可能在你的模型或單元配置中。打開zombie support以搜索此類錯誤。

0

幫助我解決類似問題的一件事情是實現了以下內容:如果它是另一個視圖的子視圖,則可能需要保留一個表視圖控制器,如果它是另一個視圖的子視圖 - 即父視圖必須以某種方式將它設置爲IBOutlet,保留孩子。

0

代替使用

細胞的= [[[ALLOC的UITableViewCell] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]自動釋放];

請使用

細胞= [[[ALLOC的UITableViewCell] initWithFrame:方法CGRectZero reuseIdentifier:CellIdentifier]自動釋放];

和更改您的實現代碼如下:

不要「的cellForRowAtIndexPath」方法使用下面的代碼。而是在「didSelectRowAtIndex」方法中使用。

在頭(.h)文件:

ProfileViewController * profileView;

在實現(.M)文件:

如果(profileView ==無) profileView = [[ProfileViewController的alloc] initWithNibName:@ 「ProfileViewController」 束:無]; profileView.user_name = username; profileView.message_source = messageSource; [self.navigationController pushViewController:profileView animated:YES];