0

我有一个TableViewController和一个SearchDisplayController附加到它。它检查当前tableView是搜索还是正常,然后将变量设置为这样,然后使用此数据填充表。这是来源:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *item;
    if(tableView == self.searchDisplayController.searchResultsTableView){
        item = [self.search objectAtIndex:indexPath.row];
    } else {
        item = [self.results objectAtIndex:indexPath.row];
    }
    static NSString *CellIdentifier = @"Cell";

    /********* ERROR HAPPENS HERE!!!!! **********/
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell"];
    /*********************************************/

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        UIImageView *iconView = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, 5.0, 44.0, 44.0)];
        iconView.tag = 100;
        [cell.contentView addSubview:iconView];

        UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(57.0, 5.0, 249.0, 29.0)];
        title.tag = 101;
        title.font = [UIFont fontWithName:@"AmericanTypewriter-Bold" size:15.0];
        title.textColor = [UIColor colorWithRed:255.0f/255.0f green:128.0f/255.0f blue:0.0f/255.0f alpha:1.0];
        [cell.contentView addSubview:title];

        UILabel *subtitle = [[UILabel alloc] initWithFrame:CGRectMake(57.0, 28.0, 249.0, 21.0)];
        subtitle.tag = 102;
        subtitle.font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:12.0];
        subtitle.textColor = [UIColor colorWithRed:179.0f/255.0f green:179.0f/255.0f blue:179.0f/255.0f alpha:1.0];
        [cell.contentView addSubview:subtitle];
    } else {
        UIImageView *iconView = (UIImageView *)[cell viewWithTag:100];
        if([[item valueForKey:@"categories"] count] == 0){
            UIImage *img = [UIImage imageNamed:@"venue"];
            iconView.frame = CGRectMake(iconView.frame.origin.x, iconView.frame.origin.y, img.size.width, img.size.height);
            [iconView setImage: img];
        } else {
            NSDictionary *iconDict = [[[item valueForKey:@"categories"] objectAtIndex:0] valueForKey:@"icon"];
            NSString *icon = [NSString stringWithFormat:@"%@bg_44%@", [iconDict valueForKey:@"prefix"], [iconDict valueForKey:@"suffix"]];
            [iconView setImageWithURL:[NSURL URLWithString:icon] placeholderImage:[UIImage imageNamed:@"venue"]];
        }
        UILabel *title = (UILabel *)[cell viewWithTag:101];
        title.text = [item valueForKey:@"name"];
        UILabel *subtitle = (UILabel *)[cell viewWithTag:102];
        subtitle.text = [[item valueForKey:@"location"] valueForKey:@"address"];
    }
    UIView *bgColorView = [[UIView alloc] init];
    [bgColorView setBackgroundColor:[UIColor colorWithRed:255.0f/255.0f green:128.0f/255.0f blue:0.0f/255.0f alpha:1.0]];
    [cell setSelectedBackgroundView:bgColorView];
    return cell;
}

每当代码遇到它试图到达的行时,我一直被一个导致应用程序崩溃的错误所困扰dequeueReusableCell。在大多数现代 iOS 设备中,它都可以工作。我已经看到它可以在我的 iPhone5、朋友的 iPhone4 和更新版本的 iPad 上运行。但是在某些设备中——包括我的第一代 iPad(安装了 iOS5)——这条线就崩溃了。没有有用的错误消息。我得到的只是典型的“(lldb)”消息。甚至不是数组越界类型的异常。堆栈跟踪仅显示它在内部中断dequeueReusableCellWithIdentifier。这是堆栈跟踪:

2013-03-17 17:32:30.350 Streetgram[861:707] *** Assertion failure in -[UINib lazyArchiveData], /SourceCache/UIKit/UIKit-1914.85/UINib.m:210
2013-03-17 17:32:30:373 Streetgram[861:1799] -[SearchViewController tableView:cellForRowAtIndexPath:] [Line 115] Uncaught exception: UINib must be instantiated with either data or a nib name.
2013-03-17 17:32:30:431 Streetgram[861:1799] -[SearchViewController tableView:cellForRowAtIndexPath:] [Line 116] Stack trace: (
0   CoreFoundation                      0x35c508a7 __exceptionPreprocess + 186
1   libobjc.A.dylib                     0x33874259 objc_exception_throw + 32
2   CoreFoundation                      0x35c50789 +[NSException raise:format:] + 0
3   Foundation                          0x350443a3 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 90
4   UIKit                               0x335c7a47 -[UINib lazyArchiveData] + 186
5   UIKit                               0x335c7b97 -[UINib unarchiverForInstantiatingReturningError:] + 78
6   UIKit                               0x335c7d8f -[UINib instantiateWithOwner:options:] + 98
7   UIKit                               0x333b17bd -[UITableView dequeueReusableCellWithIdentifier:] + 304
8   Streetgram                          0x00104157 -[SearchViewController tableView:cellForRowAtIndexPath:] + 762
9   UIKit                               0x333b0efb -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 546
10  UIKit                               0x333affd9 -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1076
11  UIKit                               0x333af763 -[UITableView layoutSubviews] + 206
12  UIKit                               0x33353f15 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 148
13  CoreFoundation                      0x35baf1fb -[NSObject performSelector:withObject:] + 42
14  QuartzCore                          0x32d7aaa5 -[CALayer layoutSublayers] + 216
15  QuartzCore                          0x32d7a6bd _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 216
16  QuartzCore                          0x32d7e843 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 226
17  QuartzCore                          0x32d7e57f _ZN2CA11Transaction6commitEv + 314
18  QuartzCore                          0x32d764b9 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 56
19  CoreFoundation                      0x35c24b1b __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 18
20  CoreFoundation                      0x35c22d57 __CFRunLoopDoObservers + 258
21  CoreFoundation                      0x35c230b1 __CFRunLoopRun + 760
22  CoreFoundation                      0x35ba64a5 CFRunLoopRunSpecific + 300
23  CoreFoundation                      0x35ba636d CFRunLoopRunInMode + 104
24  GraphicsServices                    0x33941439 GSEventRunModal + 136
25  UIKit                               0x3337ecd5 UIApplicationMain + 1080
26  Streetgram                          0x001007c9 main + 116
27  Streetgram                          0x00100750 start + 40
    ....

知道为什么会这样吗?

4

2 回答 2

2

当您将可重用单元格出列时,您应该使用在参数中传递的 tableView,而不是您自己已知的 self.tableView。对于搜索视图,这两个不会相同,这可能会导致问题。像这样:

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
于 2013-03-17T21:21:07.453 回答
1

终于找到了一个解决方案(我什至不知道它为什么起作用但它起作用的细节)。事实证明,在某些情况下,您必须取消选中“使用自动布局”设置。http://www.seifeet.com/2012/12/ios-nib-data-is-invalid-exception-with.html

于 2013-03-18T04:36:01.340 回答