滚动UITableView时的EXE访问错误 - iPhone

时间:2012-09-01 19:14:25

标签: iphone objective-c xcode uitableview

我有一个简单的UITableView,包含9个单元格。当我通过滚动向上或向下移动表格时,我得到EXE访问权限不佳。 NSZombieMode指向cellForRowAtIndexMethod。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

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

cell.textLabel.text = [lineArray objectAtIndex:indexPath.row];
cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator;

return cell;
}

任何人都可以提出错误的建议吗?

3 个答案:

答案 0 :(得分:1)

如果禁用ARC,则在创建autorelease

时添加cell
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:CellIdentifier] autorelease];

这可能是泄漏的原因。还要检查lineArray,因为它像ivar一样使用,可能这个数组在某个时候发布了。

答案 1 :(得分:1)

我的猜测是你试图访问lineArray中超出界限的元素。

IE:当您indexPath.row中只有3个元素时,lineArray返回6。

当你向下滚动时会发生这种情况,因为它会触发cellForRowAtIndexPath来调用更高数量的行(例如,带有indexPath.row> 3的行)

我会再走一步,猜测你可能静静地回归numberOfRowsForSection

将其设置为lineArray.count应修复它。

答案 2 :(得分:0)

根据我的理解: -

1)在lineArray中你有9个项目,但是在numberOfRowsInSection中你返回的rowCount比数组中的项目多,所以它崩溃并指向ceelForRowAtIndex。

2)以下是您理解的示例代码: -

- (void)viewDidLoad
{
    [super viewDidLoad];
    lineArray = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
    tableView1 = [[UITableView alloc]init];
    tableView1.delegate = self;
    tableView1.dataSource = self;
    tableView1 .frame =self.view.frame;
    [self.view addSubview:tableView1];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  return  [lineArray count];

    //return ;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

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

    cell.textLabel.text = [lineArray objectAtIndex:indexPath.row];
    cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}