gpt4 book ai didi

ios - 如何在 moveRowAt : sourceIndexPath:? 后删除空白部分

转载 作者:行者123 更新时间:2023-11-28 06:05:59 25 4
gpt4 key购买 nike

将一行拖入新节后,如果源节为空,如何删除该节?

我的代码:

var exerciseSets = [Array<Exercise>]()

func numberOfSections(in tableView: UITableView) -> Int {
return self.exerciseSets.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let set = self.exerciseSets[section]
return set.count
}

func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
var sourceSet = self.exerciseSets[sourceIndexPath.section]
let sourceExercise = sourceSet[sourceIndexPath.row]

var destinationSet = self.exerciseSets[destinationIndexPath.section]
destinationSet.insert(sourceExercise, at: destinationIndexPath.row)

sourceSet.remove(at: sourceIndexPath.row)
if sourceSet.count == 0 {
self.exerciseSets.remove(at: sourceIndexPath.section)
}
}

exerciseSets 有一组 Exercise 对象来填充每个部分的行。当一个单元格被移动时,我更新数据源,删除一个部分(从 exerciseSets 数组),如果它现在是空的。

如果一个部分是空的并且更新了数据源以反射(reflect)减少的部分数量,我仍然在屏幕上留下一个空白部分。在 moveRowAt 中重新加载 tableView 会崩溃。

最佳答案

我试图实现一个最小的工作示例来测试它是如何工作的。以下是一个工作示例,它确实使用了我之前推荐的 deleteSections:

import UIKit

class Table: UITableViewController {

var excerciseSets: [[String]] = [
["Mama", "Tato", "Baba"],
["1", "2", "3"],
["a", "b", "c"],
]

override func viewDidLoad() {
super.viewDidLoad()
tableView.setEditing(true, animated: false)
}

override func numberOfSections(in tableView: UITableView) -> Int {
return excerciseSets.count
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return excerciseSets[section].count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = excerciseSets[indexPath.section][indexPath.row]
return cell
}

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
switch section {
case 0:
return "Family"
case 1:
return "Numbers"
case 2:
return "Letters"
default:
fatalError()
}
}

override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}

override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
var sourceSet = self.excerciseSets[sourceIndexPath.section]
let sourceExercise = sourceSet[sourceIndexPath.row]

var destinationSet = self.excerciseSets[destinationIndexPath.section]
destinationSet.insert(sourceExercise, at: destinationIndexPath.row)

sourceSet.remove(at: sourceIndexPath.row)

// I had to update sets back, because an array of value types acts like a value type
// when I created variables destinationSet and sourceSet, arrays were copied, therefore
// so far I haven't really changed the model in self.excerciseSets
self.excerciseSets[destinationIndexPath.section] = destinationSet
self.excerciseSets[sourceIndexPath.section] = sourceSet

if sourceSet.count == 0 {
self.excerciseSets.remove(at: sourceIndexPath.section)

// Remove the section
tableView.beginUpdates()
tableView.deleteSections([sourceIndexPath.section], with: .none)
tableView.endUpdates()
}
}
}

现在请特别注意 moveRowAt 中的这些行:

// I had to update sets back, because an array of value types acts like a value type
// when I created variables destinationSet and sourceSet, arrays were copied, therefore
// so far I haven't really changed the model in self.excerciseSets
self.excerciseSets[destinationIndexPath.section] = destinationSet
self.excerciseSets[sourceIndexPath.section] = sourceSet

在我意识到模型并没有真正更新之前,出现了一些奇怪的行为。如果你的模型(Exercise)是一个struct,它也是一个值类型,并且通过移动行你并没有真正更新后面的模型,除非你重新分配数组.

检查您的代码并检查是否不是这种情况。

关于ios - 如何在 moveRowAt : sourceIndexPath:? 后删除空白部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48209767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com