2010-05-24 39 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];