UITableView'reloadSections:'在iOS 9中使用EXEC_BAD_ACCESS崩溃的方法

时间:2015-09-29 14:32:44

标签: objective-c uitableview ios8 exc-bad-access ios9

我正在创建一个带有展开和折叠部分的UITableView。单击一个部分时,我正在尝试将行加载到该部分,如下所示

MenuItem *menuItem = [self.menuItems objectAtIndex:indexPath.section];    
[menuItem expandInfoValue].isExpanded = YES; 
NSRange range   = NSMakeRange(indexPath.section, 1);
NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range];
[self.tableView reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationFade];

中的行数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    MenuItem *menuItem = [self.menuItems objectAtIndex:section];

    if([menuItem expandInfoValue].isExpanded)
        return [[[self.menuItems objectAtIndex:section] subMenuItems] count];
    else
        return 0;
}

以上代码在iOS8.0中运行良好。但是当我在iOS9上运行代码时,

[self.tableView reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationFade];

与EXEC_BAD_ACCESS崩溃但没有崩溃日志。 有人可以指出我可能出现的问题是正确的吗? 提前谢谢。

*****根据Philip ****

的建议添加回溯
* thread #1: tid = 0xb5883, 0x000000010d6129b5 UIKit`__46-[UITableView _updateWithItems:updateSupport:]_block_invoke + 94, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0xfffffffffffffff8)
    frame #0: 0x000000010d6129b5 UIKit`__46-[UITableView _updateWithItems:updateSupport:]_block_invoke + 94
    frame #1: 0x000000010d612b1b UIKit`__46-[UITableView _updateWithItems:updateSupport:]_block_invoke1004 + 241
    frame #2: 0x000000010d612245 UIKit`-[UITableView _updateWithItems:updateSupport:] + 2686
    frame #3: 0x000000010d60ae15 UIKit`-[UITableView _endCellAnimationsWithContext:] + 15344
    frame #4: 0x000000010d621327 UIKit`-[UITableView _updateSections:updateAction:withRowAnimation:headerFooterOnly:] + 403
  * frame #5: 0x000000010a7a9019 MenuListApp`-[BehindPanelViewController resetPrevSelectedSectionHeaderView](self=0x00007fe1dae09b10, _cmd="resetPrevSelectedSectionHeaderView") + 185 at BehindPanelViewController.m:757
    frame #6: 0x000000010a7a839d MenuListApp`-[BehindPanelViewController menuItemSectionHeaderTapped:](self=0x00007fe1dae09b10, _cmd="menuItemSectionHeaderTapped:", gestureRecognizer=0x00007fe1dacd3c10) + 285 at BehindPanelViewController.m:698
    frame #7: 0x000000010d9a4b40 UIKit`_UIGestureRecognizerSendTargetActions + 153
    frame #8: 0x000000010d9a16af UIKit`_UIGestureRecognizerSendActions + 162
    frame #9: 0x000000010d99ff01 UIKit`-[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 822
    frame #10: 0x000000010d9a73f3 UIKit`___UIGestureRecognizerUpdate_block_invoke809 + 79
    frame #11: 0x000000010d9a7291 UIKit`_UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 342
    frame #12: 0x000000010d998eb4 UIKit`_UIGestureRecognizerUpdate + 2624
    frame #13: 0x000000010d554592 UIKit`-[UIWindow _sendGesturesForEvent:] + 1137
    frame #14: 0x000000010d555681 UIKit`-[UIWindow sendEvent:] + 849
    frame #15: 0x000000010d507752 UIKit`-[UIApplication sendEvent:] + 263
    frame #16: 0x000000010a62cdde MenuListApp`-[TimerUIApplication sendEvent:](self=0x00007fe1d8613170, _cmd="sendEvent:", event=0x00007fe1d8707cd0) + 78 at TimerUIApplication.m:36
    frame #17: 0x000000010d4e2fcc UIKit`_UIApplicationHandleEventQueue + 6693
    frame #18: 0x000000010f4aa0a1 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    frame #19: 0x000000010f49ffcc CoreFoundation`__CFRunLoopDoSources0 + 556
    frame #20: 0x000000010f49f483 CoreFoundation`__CFRunLoopRun + 867
    frame #21: 0x000000010f49ee98 CoreFoundation`CFRunLoopRunSpecific + 488
    frame #22: 0x0000000111103ad2 GraphicsServices`GSEventRunModal + 161
    frame #23: 0x000000010d4e8676 UIKit`UIApplicationMain + 171
    frame #24: 0x000000010a6265c7 MenuListApp`main(argc=1, argv=0x00007fff555df568) + 151 at main.m:15
    frame #25: 0x000000010fd3492d libdyld.dylib`start + 1
    frame #26: 0x000000010fd3492d libdyld.dylib`start + 1

2 个答案:

答案 0 :(得分:2)

尝试在[self.tableview beginUpdates][self.tableview endUpdates]

中封装您的部分重新加载语句
[self.tableview beginUpdates]
MenuItem *menuItem = [self.menuItems objectAtIndex:indexPath.section];    
[menuItem expandInfoValue].isExpanded = YES; 
NSRange range   = NSMakeRange(indexPath.section, 1);
NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range];
[self.tableView reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationFade];
[self.tableview endUpdates]

答案 1 :(得分:1)

在重新加载部分之前尝试重新加载tableview,如下所示:

[self.tableView reloadData];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];

如果这没有解决您的问题,请尝试在重新加载您的部分之前和之后使用[tableview beginUpdates][tableview endUpdates]方法,如下所示:

[tableview beginUpdates];
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
[tableview endUpdates];

我遇到了类似的问题,并通过这个解决方案解决了。希望这会对你有所帮助:)。