在UITableView中保留一个占位符单元格

编程入门 行业动态 更新时间:2024-10-23 01:45:47
本文介绍了在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中保留一个占位符单元格

本文发布于:2023-10-23 02:07:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519424.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元格   UITableView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!