繁体   English   中英

有趣的UITableView数据源行为

[英]interesting UITableView Datasource behavior

所以我有一个非常基本的ipad视图控制器,并且我正在同一视图中使用多个UITableViews做一些测试。 我遇到的问题是当我选择一行时,它将抛出EXC_BAD_ACCESS,因此我打开了堆栈日志记录并发现了这一点

*** -[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

我不确定它是否比这更基本。 请告诉我,菜鸟错误非常明显,以至于我必须停止在此处发布。 提前致谢

消息发送到已释放实例0x4c0ad40

此错误表明您的VCTables实例已被释放。 UITableView仍然具有对它的引用(委托属性),因此它正在尝试向已发布的对象发送消息。 通用术语是僵尸。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM