2015-05-28 32 views
1

UISearchDisplayController是否有可能有tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法调用而不调用数据源的实际行数?UITableView重绘没有数据源调用

这里是堆栈的碰撞痕迹(的iOS 7.1.2):

Hardware Model:  iPhone4,1 

Application Specific Information: 
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' 

Last Exception Backtrace: 
0 CoreFoundation      0x3085df83 __exceptionPreprocess + 131 
1 libobjc.A.dylib      0x3b0d8ccf objc_exception_throw + 36 
2 CoreFoundation      0x307947cb -[__NSArrayM objectAtIndex:] + 228 
3 MyApp        0x0016cbb3 -[BaseContactsViewController tableView:cellForRowAtIndexPath:] (BaseContactsViewController.m:338) 
4 UIKit        0x331ad8f7 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 408 
5 UIKit        0x33154c27 -[UITableView _updateVisibleCellsNow:] + 1804 
6 UIKit        0x33147c5d -[UITableView _setNeedsVisibleCellsUpdate:withFrames:] + 166 
7 UIKit        0x33147ba3 -[UITableView _rectChangedWithNewSize:oldSize:] + 480 
8 UIKit        0x331541cd -[UITableView setBounds:] + 262 
9 UIKit        0x3317066d -[UIView(Geometry) _applyISEngineLayoutValues] + 278 
10 UIKit        0x33094549 -[UIView(Geometry) _resizeWithOldSuperviewSize:] + 162 
11 UIKit        0x33172ca3 -[UIScrollView _resizeWithOldSuperviewSize:] + 40 
12 CoreFoundation      0x307a2abd __53-[__NSArrayM enumerateObjectsWithOptions:usingBlock:]_block_invoke + 90 
13 CoreFoundation      0x307a29cb -[__NSArrayM enumerateObjectsWithOptions:usingBlock:] + 196 
14 UIKit        0x330851bd -[UIView(Geometry) resizeSubviewsWithOldSize:] + 118 
15 UIKit        0x331712e9 -[UIView(AdditionalLayoutSupport) _is_layout] + 134 
16 UIKit        0x3307afc1 -[UIView(Hierarchy) layoutSubviews] + 70 
17 UIKit        0x3307ad59 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 378 
18 QuartzCore       0x32cf862b -[CALayer layoutSublayers] + 140 
19 QuartzCore       0x32cf3e3b CA::Layer::layout_if_needed(CA::Transaction*) + 348 
20 UIKit        0x3308fe8b -[UIView(Hierarchy) layoutBelowIfNeeded] + 548 
21 UIKit        0x33129eeb -[UINavigationController _layoutViewController:] + 808 
22 UIKit        0x331ddcc5 -[UINavigationController _setNavigationBarHidden:edge:duration:] + 1270 
23 UIKit        0x3326c011 -[UINavigationController _setNavigationBarHidden:edgeIfNotNavigating:duration:] + 530 
24 UIKit        0x33196109 -[UISearchDisplayController setActive:animated:] + 8614 
25 UIKit        0x3326abcd -[UISearchDisplayController searchBarTextDidBeginEditing:] + 246 
26 UIKit        0x3326aaaf -[UISearchBar(UISearchBarStatic) _searchFieldBeginEditing] + 88 
27 UIKit        0x330b0037 -[UIApplication sendAction:to:from:forEvent:] + 88 
28 UIKit        0x330affd7 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 28 
29 UIKit        0x330affb1 -[UIControl sendAction:to:forEvent:] + 42 
30 UIKit        0x3309b717 -[UIControl _sendActionsForEvents:withEvent:] + 372 
31 UIKit        0x3309acfd -[UITextField willAttachFieldEditor:] + 666 
32 UIKit        0x33097805 -[UIFieldEditor becomeFieldEditorForView:] + 818 
33 UIKit        0x33176f15 -[UITextField _becomeFirstResponder] + 158 
34 UIKit        0x3326a91f -[UISearchBarTextField _becomeFirstResponder] + 76 
35 UIKit        0x330f9127 -[UIResponder becomeFirstResponder] + 360 
36 UIKit        0x330f93e7 -[UIView(Hierarchy) becomeFirstResponder] + 104 
37 UIKit        0x33175c17 -[UITextField becomeFirstResponder] + 44 
38 UIKit        0x333fe579 -[UISearchBar(UISearchBarStatic) becomeFirstResponder] + 34 
39 UIKit        0x33200fed _UIGestureRecognizerSendActions + 194 
40 UIKit        0x330ac4e3 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 1136 
41 UIKit        0x33453aed ___UIGestureRecognizerUpdate_block_invoke + 46 
42 UIKit        0x33073353 _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 216 
43 UIKit        0x33071a9b _UIGestureRecognizerUpdate + 280 
44 UIKit        0x330aad49 -[UIWindow _sendGesturesForEvent:] + 770 
45 UIKit        0x330aa66f -[UIWindow sendEvent:] + 664 
46 UIKit        0x3307f8cd -[UIApplication sendEvent:] + 194 
47 UIKit        0x3307df77 _UIApplicationHandleEventQueue + 7100 
48 CoreFoundation      0x3082920b __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 12 
49 CoreFoundation      0x308286db __CFRunLoopDoSources0 + 204 
50 CoreFoundation      0x30826ecf __CFRunLoopRun + 620 
51 CoreFoundation      0x30791ebf CFRunLoopRunSpecific + 520 
52 CoreFoundation      0x30791ca3 CFRunLoopRunInMode + 104 
53 GraphicsServices      0x356eb663 GSEventRunModal + 136 
54 UIKit        0x330de14d UIApplicationMain + 1134 
55 MyApp        0x0013f55b main (main.m:16) 
56 libdyld.dylib      0x3b5e5ab7 start + 0 

据我所知,有在搜索栏编辑栏和搜索水龙头开始。如果我们有来自数据源的正确行数,则不可能有这样的例外。所以我的建议是数据源没有被要求的行数。

这里是我的数据源实现:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if (_searchDisplayController.active) 
    { 
     // return number of search items 
    } 

    // return number of regular items in section 
} 

我用active属性知道如果我需要提供的搜索条目的数量,而不是常规的项目。我的另一个建议是,由于某种原因,搜索控制器在数据源调用时不活动,但在表代表调用时处于活动状态。

回答

0

是的。对于tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath,您可以返回一个空单元而不代表数据源。

tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    return cell; 
} 

您的崩溃是由BaseContactsViewController.m:338造成的。您正试图从空数据集中获取索引0处的对象。你在那条线上有什么?

+0

AFAIK if numberOfSectionsInTableView:返回零,cellForRowAtIndexPath:不会调用。所以不可能有空的数组索引异常。 –

+0

您在帖子中的建议是正确的。 UISearchDisplayController不会调用numberOfSectionsInTableView:如果您在其中设置断点,它不会中断。我在我的一个项目中体验过它。我想避免UISearchDisplayController的这种混淆。此外,它已被弃用https://developer.apple.com/library/ios/documentation/UIKit/Reference/UISearchDisplayController_Class/ – Bigman

+0

我想我现在理解你的问题。对于这个问题,我认为不可能有tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath在没有调用实际行数的数据源的情况下调用。 – Bigman