【发布时间】:2016-01-13 11:21:24
【问题描述】:
我有一个tableview 有两个部分:要购买的成分和用户已经拥有的成分。因此,基本上,当使用购物时,他/她会将一种成分从一个列表传递到另一个列表。
这里是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 - 基本上这个按钮给了我从/到的方向,我将进行移动)。
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,但是当不调用时,表索引会混淆。有必要吗?还是有别的办法?
如果reloadData 不存在,这就是行为方式。我不想重新加载所有内容并重新绘制。最佳做法是什么?
【问题讨论】:
-
“索引混淆了”到底是什么意思?发生了什么,你收到错误了吗?
-
首先当一个复选框被选中时,该行将被发送到第二部分。第二次,如果我选择第三行,它可以将第四种成分发送到“Got'em”部分。第三个复选框保持选中状态。 (我没有在第一部分检查项目)。只是好像画得不对。
-
您的
row似乎差了一位。您能否发布一些调用doChange()的代码? -
我可以发帖。但是当
reloadData被调用时一切正常,并不总是第二个消失,有时它可能会消失第三个。 -
if let delegate = checkboxDelegate, let index = checkboxIndex{ delegate.doChange(isChecked, row: index) }checkboxIndex 在 tableView:cellForRowAtIndexPath 和 indexPath.row 中设置
标签: swift checkbox tableview reloaddata