在 moveRowAtIndexPath 之后重新加载数据
reloadData after moveRowAtIndexPath
我有一个 tableview
有两个部分:要购买的原料和用户已经拥有的原料。所以,基本上当使用购物时 he/she 将一种成分从一个列表传递到另一个列表。
这里是tableView:cellForRowAtIndexPath
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("IngredientCell", forIndexPath: indexPath) as! IngredientCell
cell.checkbox.checkboxDelegate = self
cell.checkbox.checkboxIndex = indexPath
...
}
我将其命名为:
(我包含了 stateOfCheckbox - 每个 tableviewcell 都包含一个自定义按钮,其作用是 checkbox
- 基本上这个按钮给了我方向 from/to 我将采取行动)。
func doChange(stateOfCheckbox: Bool, row: Int){
let fromSection = stateOfCheckbox ? 0 : 1
let toSection = stateOfCheckbox ? 1 : 0
let fromIndexPath = NSIndexPath(forRow: row, inSection: fromSection)
let toIndexPath = NSIndexPath(forRow: 0, inSection: toSection)
let dataPiece = ingredients[fromIndexPath.section][fromIndexPath.row]
ingredients[toIndexPath.section].insert(dataPiece, atIndex: toIndexPath.row)
ingredients[fromIndexPath.section].removeAtIndex(fromIndexPath.row)
ingredientsTableView.moveRowAtIndexPath(fromIndexPath, toIndexPath: toIndexPath)
ingredientsTableView.reloadData()
}
我不想调用 reloadData
,但是如果不以某种方式调用 table 索引就会混淆。有必要吗?或者有别的方法吗?
这是 reloadData
不存在时的行为方式。我不想重新加载所有内容并重新绘制。最佳做法是什么?
您可以使用 UITableView 的方法:tableview.beginUpdates() 和 tableview.endUpdates()
func doChange(stateOfCheckbox: Bool, row: Int){
let fromSection = stateOfCheckbox ? 0 : 1
let toSection = stateOfCheckbox ? 1 : 0
let fromIndexPath = NSIndexPath(forRow: row, inSection: fromSection)
let toIndexPath = NSIndexPath(forRow: 0, inSection: toSection)
let dataPiece = ingredients[fromIndexPath.section][fromIndexPath.row]
ingredients[toIndexPath.section].insert(dataPiece, atIndex: toIndexPath.row)
ingredients[fromIndexPath.section].removeAtIndex(fromIndexPath.row)
tableview.beginUpdates()
ingredientsTableView.moveRowAtIndexPath(fromIndexPath, toIndexPath: toIndexPath)
tableview.endUpdates()
}
我很确定问题不在您在问题中发布的代码中。从我在 Gif 中看到的情况来看,必须是以下情况:
如果你勾选一个,然后又勾选它下面的一个,索引就会移动一位。实际情况:它仍然使用第一个检查之前的旧索引。这些索引要么需要刷新,要么——我更喜欢——确定你实际点击它的时间。
我有一个 tableview
有两个部分:要购买的原料和用户已经拥有的原料。所以,基本上当使用购物时 he/she 将一种成分从一个列表传递到另一个列表。
这里是tableView:cellForRowAtIndexPath
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("IngredientCell", forIndexPath: indexPath) as! IngredientCell
cell.checkbox.checkboxDelegate = self
cell.checkbox.checkboxIndex = indexPath
...
}
我将其命名为:
(我包含了 stateOfCheckbox - 每个 tableviewcell 都包含一个自定义按钮,其作用是 checkbox
- 基本上这个按钮给了我方向 from/to 我将采取行动)。
func doChange(stateOfCheckbox: Bool, row: Int){
let fromSection = stateOfCheckbox ? 0 : 1
let toSection = stateOfCheckbox ? 1 : 0
let fromIndexPath = NSIndexPath(forRow: row, inSection: fromSection)
let toIndexPath = NSIndexPath(forRow: 0, inSection: toSection)
let dataPiece = ingredients[fromIndexPath.section][fromIndexPath.row]
ingredients[toIndexPath.section].insert(dataPiece, atIndex: toIndexPath.row)
ingredients[fromIndexPath.section].removeAtIndex(fromIndexPath.row)
ingredientsTableView.moveRowAtIndexPath(fromIndexPath, toIndexPath: toIndexPath)
ingredientsTableView.reloadData()
}
我不想调用 reloadData
,但是如果不以某种方式调用 table 索引就会混淆。有必要吗?或者有别的方法吗?
这是 reloadData
不存在时的行为方式。我不想重新加载所有内容并重新绘制。最佳做法是什么?
您可以使用 UITableView 的方法:tableview.beginUpdates() 和 tableview.endUpdates()
func doChange(stateOfCheckbox: Bool, row: Int){
let fromSection = stateOfCheckbox ? 0 : 1
let toSection = stateOfCheckbox ? 1 : 0
let fromIndexPath = NSIndexPath(forRow: row, inSection: fromSection)
let toIndexPath = NSIndexPath(forRow: 0, inSection: toSection)
let dataPiece = ingredients[fromIndexPath.section][fromIndexPath.row]
ingredients[toIndexPath.section].insert(dataPiece, atIndex: toIndexPath.row)
ingredients[fromIndexPath.section].removeAtIndex(fromIndexPath.row)
tableview.beginUpdates()
ingredientsTableView.moveRowAtIndexPath(fromIndexPath, toIndexPath: toIndexPath)
tableview.endUpdates()
}
我很确定问题不在您在问题中发布的代码中。从我在 Gif 中看到的情况来看,必须是以下情况:
如果你勾选一个,然后又勾选它下面的一个,索引就会移动一位。实际情况:它仍然使用第一个检查之前的旧索引。这些索引要么需要刷新,要么——我更喜欢——确定你实际点击它的时间。