首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TableView在tableView皮疹中

TableView在tableView皮疹中
EN

Stack Overflow用户
提问于 2015-06-28 22:25:39
回答 1查看 68关注 0票数 0

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

viewDidLoad:

代码语言:javascript
复制
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 (动态原型)上滚动时,我会得到以下错误:

代码语言:javascript
复制
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,我能做些什么来修复它呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-28 22:40:30

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

您的数据数组只有两个元素,但是您正在硬编码要创建的单元格数量(3),所以当它查找第三个元素创建单元格时,它会因为不存在而崩溃。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31105387

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档