【问题标题】:Custom Cell load NIb work on ios 6 but crashed on ios 5自定义单元加载 NIb 在 ios 6 上工作,但在 ios 5 上崩溃
【发布时间】:2013-03-26 00:28:47
【问题描述】:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CustomCell";
    CustomCell *cell = (CustomCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

    }

在 ios 6 模拟器上完美运行,但在 ios 5 模拟器上崩溃并出现以下错误: 'NSInternalInconsistencyException', reason: 'NIB 数据无效。

苹果有什么改变吗?

【问题讨论】:

    标签: xcode uitableview xcode4.5 loadnibnamed


    【解决方案1】:

    这里有一些建议!

    如果您使用最新版本的 Xcode(Xcode 4.5 或 4.6)创建此项目,CustomCell 笔尖默认打开自动布局。但是 iOS 5 中没有自动布局。

    另外,您的代码在 iOS 5 和 iOS 6 中都是错误的。如果您从笔尖获取单元格,请不要使用 loadNibNamed:。相反,使用registerNib:forCellReuseIdentifier: 将笔尖注册到表格视图。现在,当您调用dequeueReusableCellWithIdentifier: 时,如有必要,笔尖将自动加载。请注意,此 nib 中必须只有 一个 顶级对象,并且必须是单元格。这是一个愚蠢的规则,但事实就是如此。

    【讨论】:

    • 感谢您的回答。我未选中自动布局,现在单元格已加载,但是当我单击一行时出现此错误:'NSInvalidUnarchiveOperationException',原因:'无法实例化名为 NSLayoutConstraint 的类'并且..我对 ios 开发很陌生,所以我不太明白我应该用这个方法做什么 registerNib:forCellReuseIdentifier: 如果你能给我一个示例代码,我会很高兴。
    • 关于错误,我已修复。如果你能解释一下 registerNib:forCellReuseIdentifier: 它会很好..
    • 嗨@user1600694,这里是示例代码:github.com/mattneub/Programming-iOS-Book-Examples/blob/master/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    • 2012-06-27
    • 2012-02-09
    • 2012-02-05
    相关资源
    最近更新 更多