在UITableView中保留一个占位符单元格 [英] Keeping a placeholder cell in a UITableView

查看:76
本文介绍了在UITableView中保留一个占位符单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITableView,我永远不想低于1个单元格:这是一个目录读出,并且如果目录中没有文件,则它只有一个单元格,显示无文件". (在编辑模式下,有一个用于创建文件的奖励单元格,因此,编辑模式永远不会低于两个单元格.)

I have a UITableView that I never want to fall below 1 cell: It's a directory readout, and if there's no files in the directory, it has a single cell that says "No Files". (In edit mode, there's a bonus cell to Create a File, so edit mode never falls below two cells.)

可能只是睡眠不足,使我无法立即思考从纸袋中出来的方法,但是我一直在遇到类似这样的错误:

It's probably just lack of sleep keeping me from thinking my way out of a paper bag right now, but I keep tripping up on errors like this:

*** Terminating app due to uncaught exception 
'NSInternalInconsistencyException', reason: 'Invalid update: 
invalid number of sections.  The number of sections contained 
in the table view after the update (2) must be equal to 
the number of sections contained in the table view before 
the update (2), plus or minus the number of sections inserted 
or deleted (1 inserted, 0 deleted).'

之所以发生这种情况,是因为我在删除最后一个文件之前先添加了"No Files"占位符.如果我在添加占位符之前删除了最后一个文件单元,则会发生类似的崩溃.无论哪种方式,单元格计数都与numberOfRowsInSection的返回值不同步,从而触发崩溃.

This is happening as I'm preemptively adding the "No Files" placeholder before deleting the last file. There's a like crash if I delete the last file cell before adding the placeholder. Either way, the cell count gets out of sync with the return from numberOfRowsInSection, and that triggers a crash.

当然有针对这种情况的设计模式.提示我进来吗?

Surely there's a design pattern for this situation. Clue me in?

推荐答案

按照下面的代码片段所示的方式进行操作:

Do something along the lines shown in the code snippet below:

  • 首先从数组中删除该行的数据
  • 如果数组项未降为零,则从表中删除行
  • 如果数组项已降为零,则重新加载表-注意:现在,您的代码现在应该为行数提供1,并将行0的单元格配置为在调用tableview委托方法时显示无文件".
 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // First delete the row from the data source
        [self deleteTableData: indexPath.row];  // method that deletes data from 
        if ([self.tableDataArray count] != 0) {
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        } else {
            [self.tableView reloadData];
        }

    }   
}

这篇关于在UITableView中保留一个占位符单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆