2012-09-01 45 views
1

我有一个简单的UITableView与9个单元格。当我通过滚动向上或向下移动表格时,EXE将无法访问。 NSZombieMode指向cellForRowAtIndexMethod。滚动UITableView时EXE访问不良 - iPhone

- (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; 
} 

任何人都可以提出什么是错的?

回答

1

如果ARC被禁止再加入autorelease当您创建cell

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

这可能是泄漏的原因。请检查lineArray,因为它使用像伊娃这样的数据,并且可能在某个时候发布了这个数组。

1

我的猜测是您正试图访问您的lineArray中出界的元素。

IE:indexPath.rowlineArray中只有3个元素时返回6。

它发生时,你向下滚动,因为它触发cellForRowAtIndexPath被称为较高的行数(行与indexPath.row> 3为例)

我会去一个步骤,你猜,你是可能静态返回numberOfRowsForSection

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

+1

不,这绝对不是这样 - 阵列工作正常。当我从滚动中释放手指时发生崩溃 - 所以当它试图重新加载滚动离开屏幕的单元格时,它看起来像崩溃了。我想知道这是否与UITableView和ARC的错误。 – GuybrushThreepwood

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 
{ 

}