2017-06-05 60 views
-1

排序表视图后我有启用多个选择的表图。用户应该能够在列表中选择多个选项,然后更改列表的排序顺序。我希望能够保持选择了相同的细胞,但他们的订单将会从排序变化。我没有看到一种方法来实现这一点,所以任何建议表示赞赏。保持选择的细胞在夫特

+0

你为什么不使用有一个属性'定制单元选择“,并以你想要的方式切换该属性?这样你就可以知道在使用'selected'属性对表视图进行排序后选择了哪些单元格。 –

+0

我才有打完电话后同样参照细胞tableView.reloadData()? – user2325848

回答

0

如果你在你的数据结构中的“选择”的指标,你可以用它来重建的tableView选择。

如果不能这样的指标添加到数据,你可以分两步执行您的排序:1)计算的新元素的索引,2)这些指标适用于你的数据数组进行排序。

进行排序指标,可以重建在原有基础上索引的新位置的选择:

例如:

// Assuming your tableview is named "tableView", 
// and the data array is named "dataArray", 
// and you sort uses a member of dataArray called "sortKey" 


// prepare list of new indexes for sorted data 
let newIndexes = dataArray.enumerated().sorted{ $0.1.sortKey < $1.1.sortKey }.map{$0.0} 

// determine new indexes of current selection 
let newSelection = tableView.indexPathsForSelectedRows? 
        .map{IndexPath(row:newIndexes.index(of:$0.row)!, section:0)} 

// sort and reload the data        
dataArray  = newIndexes.map{ self.dataArray[$0] } 
tableView.reloadData() 

// reapply the selection 
tableView.indexPathsForSelectedRows?.forEach{tableView.deselectRow(at:$0, animated:false)} 
for indexPath in newSelection ?? [] 
{ self.tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none) }