dequeueReusableCellWithIdentifier在iPad上崩溃(使用iOS5)

时间:2013-03-17 21:07:45

标签: ios objective-c ipad uitableview

我附加了TableViewControllerSearchDisplayController。它检查当前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
    ....

知道为什么会这样吗?

2 个答案:

答案 0 :(得分:2)

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

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

答案 1 :(得分:1)

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