2010-08-27 29 views
1

所以我有这个非常基本的ipad视图控制器,我正在做一些测试与多个UITableViews在同一视图。我在当时我选择了一排,它会抛出一个EXC_BAD_ACCESS的问题,所以我把堆栈记录,发现这个有趣的UITableView数据源行为

*** -[VCTables tableView:didSelectRowAtIndexPath:]: message sent to deallocated instance 0x4c0ad40 

然后我就开始看我的代码和无法弄清楚的生活我是什么导致它。我有我的视图控制器上的UITableViewDataSource和UITableViewDelegate并具有所有适当的代码来处理索引选择行。它正确地绘制了表格,它似乎并没有将自己作为数据源。

我试过在代码中构建UITableViews,并且在界面构建器中也是这样。在卸载它并重新启动后,我重新下载并安装了XCode 3.2.3 SDK 4.0.2。我不能为了我的生活看到我失去了什么,但在做了之前,我现在确信(这是一个代码问题,而不是IDE),我只是无法打开我的眼睛足以看到代码问题。

此外,这也只发生在一张桌子上。并与2个表,它发生,无论我选择哪个表

这里是一些代码:

VCTables.h

#import <UIKit/UIKit.h> 
@interface VCTables : UIViewController<UITableViewDelegate,UITableViewDataSource> { 
    UITableView *table1; 
    UITableView *table2;  
} 
@end 

VCTables.m

#import "VCTables.h" 
@implementation VCTables 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    return 50; 
} 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return 7; 
} 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *CellIdentifier = [[NSString alloc] initWithString:@"yourCell"]; 
    if(tableView.tag == 2000){ 
     [CellIdentifier release]; 
     CellIdentifier = [[NSString alloc] initWithString:@"myCell"]; 
    } 
    UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    if(tableView.tag == 2000){ 
     [cell.textLabel setText:[NSString stringWithFormat:@"%d",indexPath.row]]; 
    }else{ 
     [cell.textLabel setText:[NSString stringWithFormat:@"%d%d",indexPath.row,indexPath.section]]; 
    } 
    [CellIdentifier release]; 
    return cell; 
} 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
    return NO; 
} 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { 
    return NO; 
} 
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath { 
    NSLog(@"selected"); 
} 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    table1 = [[UITableView alloc] initWithFrame:CGRectMake(20, 73, 320, 480) style:UITableViewStylePlain]; 
    table1.delegate=self; 
    table1.dataSource=self; 
    table1.tag=2000; 
    [self.view addSubview:table1]; 
    table2 = [[UITableView alloc] initWithFrame:CGRectMake(483, 73, 320, 480) style:UITableViewStylePlain]; 
    table2.delegate=self; 
    table2.dataSource=self; 
    table2.tag=2000; 
    [self.view addSubview:table2]; 
} 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return YES; 
} 
- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
} 
- (void)viewDidUnload { 
    [super viewDidUnload]; 
} 
- (void)dealloc { 
    [super dealloc]; 
} 
@end 

我不是肯定它比这更基本。请告诉我这个如此痛苦明显的菜鸟错误,我必须停止在这里发帖。在此先感谢

+0

尝试在创建表格时保留表格 - 正常的EXC_BAD_ACCESS表示某些东西在您使用它时自动释放/遗忘。 :)你还需要记住在 - (void)dealloc中释放你的表。 – 2010-08-27 15:19:50

+0

是的,这只是用于SO目的的修剪代码。问题在于VC本身被释放了一层以上,所以实际上没有VCTables对象成为视图中引发表的委托。 – AtomRiot 2010-08-27 15:24:47

回答

3

消息发送到释放实例0x4c0ad40

这个错误表明您VCTables实例已被释放。 UITableView仍然有一个对它的引用(委托属性),所以它试图发送消息给一个发布的对象。这个常见术语是僵尸。

为了调试,你应该看看如何为你的VCTables对象完成内存管理。它在哪里创建,谁拥有它?

如果您可以使用Apple的性能工具,请尝试使用僵尸工具找出VCTables对象的发布位置。

+0

omg,说到僵尸,我发VCTables查看,然后刚刚释放它,因为我只是alloc'd它,并没有进一步思考...谢谢你让我放心,我不是疯了! – AtomRiot 2010-08-27 15:23:26

+0

不用担心。我们都在那里':)' – 2010-08-27 15:26:31