【问题标题】:How to shift cell after select it选择后如何移动单元格
【发布时间】:2016-05-02 19:03:05
【问题描述】:

我正在锁定方法中的解决方案

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: UIIndexPath) {
}

在我选择一个行后,它应该改变颜色并移动到数组的末尾,另一个应该向上移动

【问题讨论】:

  • 上下移动时是否需要应用动画?

标签: ios swift uitableview cells


【解决方案1】:

要将行向下移动,您可以:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: UIIndexPath) {
    //move selected cell down to last row
    tableView.moveRowAtIndexPath(indexPath, toIndexPath:NSIndexPath(forRow: tableView.numberOfRowsInSection(indexPath.section)-1, inSection: indexPath.section))
}

更改颜色见How to change color of UITableViewCell when selecting?

【讨论】:

  • 非常感谢,它很有效,而且很简单,这几天我一直在寻找解决方案:)
【解决方案2】:

为了数据的一致性,您必须分别移动表中的项目和数据源数组。

dataSourceArray代表数据源数组

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

  let itemToMove = dataSourceArray[indexPath.row]
  dataSourceArray.removeAtIndex(indexPath.row)
  dataSourceArray.append(itemToMove)

  let destinationIndexPath = NSIndexPath(forRow: dataSourceArray.count - 1, inSection: indexPath.section)
  tableView.moveRowAtIndexPath(indexPath, toIndexPath:destinationIndexPath)
}

【讨论】:

  • 感谢您的提示,但是如果我的数组中有很多元素并且看不到最后一个,请单击第一个,它会移动到数组的末尾并变成灰色背景,但是如果我向下滚动并查看最后一个元素,然后在最后一个元素之前选择一个,它会正确向下移动而不改变颜色。你现在知道为什么会这样吗??
  • 滚动会显式调用cellForRowAtIndexPath。如果它是可见的,你可以添加一行来重新加载这个特定的行。
  • 也许是一种方法,我有点绝望
猜你喜欢
  • 2020-02-12
  • 1970-01-01
  • 2014-06-11
  • 1970-01-01
  • 2018-10-12
  • 2022-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多