首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >reloadData后moveRowAtIndexPath

reloadData后moveRowAtIndexPath
EN

Stack Overflow用户
提问于 2016-01-13 11:21:24
回答 2查看 371关注 0票数 0

我有一个tableview,有两个部分:要购买的成分和用户已经拥有的成分。所以,基本上,当购物时,他/她会把一种配料从一张单子传递到另一张。

这是tableView:cellForRowAtIndexPath

代码语言:javascript
复制
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 -每个桌面视图单元格都包含一个带有checkbox角色的自定义按钮--基本上这个按钮给了我从/到我要做的移动的方向)。

代码语言:javascript
复制
 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,这就是is的行为方式。我不想重新装填所有的东西再重新油漆。什么是最佳做法?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-01-14 12:01:50

我很确定问题不在你在问题中发布的代码中。根据我在“Gif”中看到的情况,情况必须如下:

如果您检查一个,然后检查下面的一个,索引将被移动一个。实际情况是:它仍然使用第一个索引检查之前的旧索引。这些指数要么需要刷新,要么--或者--我更喜欢--在你实际使用它的时候决定。

票数 1
EN

Stack Overflow用户

发布于 2016-01-13 11:38:57

您可以使用UITableView的方法:tableview.beginUpdates()tableview.endUpdates()

代码语言:javascript
复制
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()

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34765330

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档