代码之家  ›  专栏  ›  技术社区  ›  AtomRiot

有趣的UITableView数据源行为

  •  1
  • AtomRiot  · 技术社区  · 15 年前

    所以我有一个非常基本的ipad视图控制器,我在同一个视图中用多个uitableview做了一些测试。我遇到的问题是,当我选择一行时,它会抛出一个EXC\u BAD\u访问,所以我打开堆栈日志,发现了这个问题

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

    我尝试过在代码中构建uitableview,也尝试过在Interface builder中构建uitableview。在卸载并重新启动XCode 3.2.3 SDK 4.0.2之后,我重新下载并安装了它。我一辈子都看不到我遗漏了什么,但在做了前面的工作之后,我现在确信(我猜)这是一个代码问题,而不是IDE,我就是睁不开眼睛看不到代码问题。

    而且,只有一张桌子也会发生这种情况。对于2个表,无论我选择哪个表都会发生

    下面是一些代码:

    VCH表格

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

    VCM表格

    #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
    

    我不确定它是否比这更基本。请告诉我菜鸟的错误,这是如此痛苦明显,我必须停止张贴在这里。提前谢谢

    1 回复  |  直到 15 年前
        1
  •  3
  •   James Eichele Bernard Igiri    15 年前

    消息已发送到已解除分配的实例0x4c0ad40

    此错误表示VCTables实例已被释放。UITableView仍然有对它的引用(delegate属性),因此它正在尝试向已发布的对象发送消息。通常的说法是僵尸。

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