【问题标题】:Why does my tableview crash on identifier?为什么我的 tableview 在标识符上崩溃?
【发布时间】:2013-05-26 05:28:12
【问题描述】:

我创建了一个 tableviewcontroller 来处理 tableview 中的 4 个静态单元格。表格视图位于 4 个单元格的正下方。但是,最后一个单元格,即第 4 个单元格,根据第 3 个单元格的结果是可选的。

选择器返回一个值,当它返回时,如果值正确,它会重新加载表格视图以激活第 4 个单元格。

当我运行应用程序时,它会在加载 SettingsViewController 时崩溃并出现以下错误:

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法将具有标识符 Cell 的单元格出列 - 必须为标识符注册一个 nib 或类,或者在情节提要中连接原型单元格”

如果我有静态单元格,我认为我不需要重用标识符?

以下是相关代码:

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(self.showLastCell)
    {
        return 4;
    }
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...

    return cell;
}

【问题讨论】:

    标签: ios uitableview reuseidentifier


    【解决方案1】:

    你不需要出队,正如你所说,它只是静态单元格。

    而不是:

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    

    使用:

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    

    祝你好运!

    编辑:太慢了!

    【讨论】:

      【解决方案2】:

      出列单元格用于具有动态单元格的表格视图。静态单元格表​​视图不必具有数据源 cellForRowAtIndexPath。因此,您可以安全地删除数据源方法 cellForRowAtIndexPath。

      查看这个问题以获得更多解释:Storyboard static cells: dequeueReusableCellWithIdentifier returns nil

      或者你可以使用这里提到的超级方法:UITableView with static cells without cellForRowAtIndexPath. How to set clear background?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-02
        • 1970-01-01
        相关资源
        最近更新 更多