UITableView重绘没有数据源调用

时间:2015-05-28 18:42:44

标签: ios uitableview uisearchdisplaycontroller

使用UISearchDisplayController是否可以在不调用实际行数的数据源的情况下进行tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法调用?

这是崩溃的堆栈跟踪(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属性来了解我是否需要提供多个搜索项而不是常规项。我的另一个建议是,由于某种原因,搜索控制器在数据源调用时没有激活,但在表委托调用时是活动的。

1 个答案:

答案 0 :(得分:0)

是。对于tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath,您只需返回一个空单元格而不表示数据源。

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

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