如果想要在表格视图中删除项目后,使得删除的项目所在的空白背景空间消失,可以通过以下代码示例解决:
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// 删除数据源中的项目
yourDataSourceArray.remove(at: indexPath.row)
// 删除表格视图中的行
tableView.deleteRows(at: [indexPath], with: .fade)
// 刷新表格视图
tableView.reloadData()
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// 返回数据源数组的数量
return yourDataSourceArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// 创建和配置单元格
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = yourDataSourceArray[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
// 如果数据源数组为空,则设置表格视图的背景视图为一个空白的视图
if yourDataSourceArray.isEmpty {
let emptyView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.width, height: tableView.bounds.height))
return emptyView
} else {
return nil
}
}
通过这样的实现,当你删除表格视图中的项目后,如果数据源数组为空,表格视图的背景视图将会变为一个空白的视图,从而消除了删除项目后的空白背景空间。
上一篇:表格事件监听器生成行不立即工作
下一篇:表格视图背景的模糊效果不起作用