2013-05-08 48 views
0

当滚动tableview时,我收到EXC_BadAccess错误消息。滚动tableview时获取EXC_Bad_Access

以下是我在cellForRowAtIndexPath中完成的代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *[email protected]"customCellHistory"; 

    customCellHistory *cell=(customCellHistory*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     NSArray *topLevelObjects=[[NSBundle mainBundle]loadNibNamed:@"customCellHistory" owner:self options:nil]; 
     for (id currentObject in topLevelObjects) { 
      if ([currentObject isKindOfClass:[UITableViewCell class]]) { 
       cell=(customCellHistory*)currentObject; 
       break; 
      } 
     } 
    } 

    cell.lb11.text=[cellArray1 objectAtIndex:indexpath.row]; 
    cell.lbl2.text=[cellArray2 objectAtIndex:indexpath.row]; 

    return cell; 
} 

我能感觉到问题是由于上面的代码中的一些错误而引起的。

我在上面的代码中使用了CustomCell来显示自定义的单元格。

谁能告诉我,我是用这个代码

回答

1

嘿尝试下面的代码,不要忘记set the cell identifier在您自定义的XIB到customCellHistory

在顶部

#import "customeCellHistory.h" 

然后

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *cellIdentifier = @"customCellHistory"; 

    customCellHistory *cell = (customCellHistory *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"customCellHistory" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
    } 

    cell.lb11.text=[cellArray1 objectAtIndex:indexpath.row]; 
    cell.lbl2.text=[cellArray2 objectAtIndex:indexpath.row]; 

    return cell; 
} 
+0

嗨,我收到错误“[__NSArrayI objectAtIndex:]:发送到释放实例的消息”。 – surendher 2013-05-09 10:24:57

+0

你什么时候得到这个错误? – icodebuster 2013-05-09 11:58:32

+0

您是否创建了自定义单元格NIB并将其cellIdentifier设置为'customCellHistory' – icodebuster 2013-05-09 12:02:03

0

这个问题似乎来自你有奇怪的循环,产生做了什么错。使用最后一个对象方法从笔尖设置单元格。

0

您正在重复使用单元格,然后更改循环中的单元格,这可能会导致单元格不存在于您正在查找的位置。

0
if (cell == nil) { 
    NSArray *topLevelObjects=[[NSBundle mainBundle]loadNibNamed:@"customCellHistory" owner:self options:nil]; 

您不需要该代码。只需注册与表视图笔尖,早前:

[self.tableView registerNib:[UINib nibWithNibName:@"customCellHistory" bundle:nil] 
forCellReuseIdentifier:@"customCellHistory"]; 

现在当你调用dequeueReusableCellWithIdentifier,笔尖会被加载和小区将交付,如果在复用一堆没有备用电池。这确保您始终拥有一个单元格,并且它是正确的单元格。使用框架为您提供的方法。

+0

这取决于您的部署目标设置。如果你只支持iOS 5和6,你的答案是正确的,但如果你想支持iOS 4.x,原始代码就是实现它的方法。 – hagi 2013-05-08 17:52:13

+0

原始代码可能是在iOS 4上执行的一种方式。但这并不能使其成为*方式。这是另一种方式,我曾经在iOS 4上使用过(比OP的代码更安全,更简单,也更高效):https://github.com/mattneub/Programming-iOS-Book-Examples/blob /master/iOS4bookExamples/p516p531addCellSubviewsNibOutlets/p516addCellSubviewsNibOutlets/RootViewController.m – matt 2013-05-08 17:58:00