iphone - 在 iOS 的 tableView 中向上滚动时(当单元格消失时)Exc_bad_access

标签 iphone ios ios5 uitableview exc-bad-access

我正在使用启用了 ARC 和 Storyboard的 Xcode 4.4.1(以防万一)

我有一个带有 TableView 的 UITableViewController( TableView 使用“副标题”单元格)

我正在使用 NSArray 来填充我的表格:

@property (strong, nonatomic) NSArray *myData;

我在viewDidLoad中获取了这个表中的数据

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.myData = [self.myCalendarModel GetWeightHistory] ;
}

然后我有:numberOfSectionsInTableView 和 numberOfRowsInSection

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.myData count];
}

最后是我的 cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"WeighDataCell";
    UITableViewCell *cell = [tableView
                         dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    WeightHistory *myDataForCell = [self.myData objectAtIndex:indexPath.row];

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd.MM.YYYY"];
    NSString *dateString = [dateFormat stringFromDate:myDataForCell.weightDate];

    cell.textLabel.text = dateString;
    cell.detailTextLabel.text = [myDataForCell.weight description];

    return cell;
} 

我可以毫无问题地展示我的表格。我有空间显示 6 个单元格,我的 NSArray 有 6 条记录。当我在我的 table 上向下滚动时,我没有任何问题。

当我向上滚动时,如果没有单元格超出 View ,我也没有问题。当我松开手指时,只要有一个单元格不在 View 中,我就会收到 Exc_bad_access 错误。

当使用 NSZombieEnable 进行调试时,我可以看到:

[CalendarHistoryTableViewController tableView:cellForRowAtIndexPath:]: message sent to deallocated instance 0x6eaf6f0

所以我想我的手机被释放了,这就是我遇到这个问题的原因。但我不知道何时以及如何避免这种情况。

感谢您提供的任何帮助! 埃里克

@FaddishWorm 是的,标识符已设置,如果单元格不为零,则表示它已分配。但这部分似乎可以正常工作,因为我可以将数据显示在屏幕上。

@Pandey_Laxman 感谢您的评论。这几乎是我在这个类中拥有的唯一代码。要检查问题是否与我的 WeightHistory 对象无关,我已经从代码中删除了这部分,但我仍然遇到相同的错误

这就是我的新 cellForRowAtIndexPath 的样子:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"WeighDataCell";
    UITableViewCell *cell = [tableView
                         dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    cell.textLabel.text = @"test";
    cell.detailTextLabel.text = @"test2";
    return cell;
}

最佳答案

我想通了。

我的 TableViewController 被另一个 ViewController 中的 segue 显示在屏幕上,我没有将当前的 TableViewController 指针存储在 `强大的属性。

因此,当 iOS 尝试在我的 TableViewController 上调用 cellForRowAtIndexPath 时,它无法调用,因为它已经被释放。

感谢您的帮助。

问候,埃里克

关于iphone - 在 iOS 的 tableView 中向上滚动时(当单元格消失时)Exc_bad_access,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12052730/

相关文章:

ios - 通过xcode5的xib设计时导航栏按钮item图片颜色不同

iphone - RestKit 应用程序因 [NSPathStore2 stringByAppendingQueryParameters :]: unrecognized selector message 而崩溃

iphone - iOS 5 - AFNetworking - 上传视频

iphone - 在 UITableView 中使用 UITableViewCell

iphone - 是否可以使用 IBOutlet 数组之类的东西?

iphone - iPhone 应用程序的 iPad 内容比例因子 x2

ios - 递归函数或 block 的堆栈和堆条件?

iphone - ECSliding,在LeftViewController下更改

ios - iOS html/xml用TFHpple解析Google购物结果

ios - 使用 NSManagedObjectContextWillSaveNotification 识别已更改对象的旧值和新值