2015-06-28 112 views
0

我有static UItableView。在其中一个cells中,我有一个dynamic prototype UITableView。下面是我的实现代码:TableView在tableView中的崩溃

viewDidLoad:

self.tagsArray = [[NSArray alloc] initWithObjects:@"hey", @"what", @"ola", @"dada", @"hoster", @"umi", nil]; 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return self.tableView == tableView ? 2 : 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (self.tableView == tableView) 
     return (section == 0) ? 3 : 2; 
    else 
     return [self.tagsArray count]; 
} 

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

    if (self.tableView == tableView) { 
     cell = [super tableView:tableView cellForRowAtIndexPath:indexPath]; 
    } 
    else { 
     cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; 
     [cell.textLabel setText:self.tagsArray [indexPath.row]]; 
    } 

    return cell; 
} 

当我运行应用程序,一切工作正常。但是,当我开始滚动在内tableView(动态原型之一),然后我得到了以下错误:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]' 
*** First throw call stack: 
(
    0 CoreFoundation      0x00000001012a7c65 __exceptionPreprocess + 165 
    1 libobjc.A.dylib      0x0000000100f40bb7 objc_exception_throw + 45 
    2 CoreFoundation      0x000000010119e17e -[__NSArrayI objectAtIndex:] + 190 
    3 UIKit        0x0000000101e12132 -[UITableViewDataSource tableView:indentationLevelForRowAtIndexPath:] + 106 
    4 UIKit        0x00000001019dc1f9 __53-[UITableView _configureCellForDisplay:forIndexPath:]_block_invoke + 1711 
    5 UIKit        0x000000010195a68e +[UIView(Animation) performWithoutAnimation:] + 65 
    6 UIKit        0x00000001019dbb3b -[UITableView _configureCellForDisplay:forIndexPath:] + 312 
    7 UIKit        0x00000001019e3a41 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 533 
    8 UIKit        0x00000001019c2248 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2853 
    9 UIKit        0x00000001019d88a9 -[UITableView layoutSubviews] + 210 
    10 UIKit        0x0000000101962a2b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 536 
    11 QuartzCore       0x0000000100934ec2 -[CALayer layoutSublayers] + 146 
    12 QuartzCore       0x00000001009296d6 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380 
    13 QuartzCore       0x0000000100929546 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 
    14 QuartzCore       0x0000000100895886 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242 
    15 QuartzCore       0x0000000100896a3a _ZN2CA11Transaction6commitEv + 462 
    16 QuartzCore       0x00000001008970eb _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89 
    17 CoreFoundation      0x00000001011daca7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23 
    18 CoreFoundation      0x00000001011dac00 __CFRunLoopDoObservers + 368 
    19 CoreFoundation      0x00000001011d0a33 __CFRunLoopRun + 1123 
    20 CoreFoundation      0x00000001011d0366 CFRunLoopRunSpecific + 470 
    21 GraphicsServices     0x000000010510da3e GSEventRunModal + 161 
    22 UIKit        0x00000001018e2900 UIApplicationMain + 1282 
    23 myApp        0x00000001004cf51f main + 111 
    24 libdyld.dylib      0x0000000102cfe145 start + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 

我不知道上面的错误意味着,但我认为这不是创造新的cells出于某种原因,因此,它与要显示的数量cells发生冲突,所以它会因上述错误而崩溃。

为什么不是内部tableView创建新的cells,我该怎么办才能解决它?

+0

嵌套表视图可能会变得怪异。它看起来像UITableViewDataSource tableView:indentationLevelForRowAtIndexPath:正在做越界访问,并且该方法可能在超类中实现。我想知道是否重构,以便不同的表视图具有不同的委托实现会有所帮助。 –

回答

0

错误告诉你的是你试图访问一个不存在的元素。

你的数据数组只有2个元素,但是你要硬编码你想创建的单元的数量(3),所以当它寻找第三个元素来创建它崩溃的单元时,因为不存在。

+0

感谢您的回答!我的数组有5个对象。这是'self.tagsArray' – Jessica