我有一个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委托方法时显示无文件".
更多推荐
在UITableView中保留一个占位符单元格
发布评论