Ios Tableview滚动更改选择
我看了几个与此相关的StackOverflow问题,但似乎还没有很好地理解这个概念 我有一个带有几个单元格的tableview,我使用枚举填充这些单元格。当我向下滚动行时,表中较低的另一行被选中。我实施了以下方法:Ios Tableview滚动更改选择,ios,swift,Ios,Swift,我看了几个与此相关的StackOverflow问题,但似乎还没有很好地理解这个概念 我有一个带有几个单元格的tableview,我使用枚举填充这些单元格。当我向下滚动行时,表中较低的另一行被选中。我实施了以下方法: override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = tableVie
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("activityCell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = activities[indexPath.item].rawValue;
return cell;
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var cell = tableView.cellForRowAtIndexPath(indexPath);
cell?.selected = true;
}
我知道电池是可重复使用的,但我无法正确理解这个概念及其应用
你能给我一些帮助吗
干杯如果要重用单元格,请在indexPath的
DidSelectRowatingIndexPath
中将单元格设置为selected
,这样可以使重用的单元格也反映更改的selected
属性。相反,我建议在活动对象中添加一个布尔selected
属性,以便可以在cellforrowatinexpath:
,例如:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("activityCell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = activities[indexPath.item].rawValue
cell?.selected = activities[indexPath.item].selected
return cell;
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
activities[indexPath.item].selected = true
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
}
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
activities[indexPath.item].selected = false
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
}
正如Byron所建议的,我们可以通过保存选定的
索引数组中的路径,并在未选中时将其从数组中删除
在加载表格之前初始化indexArray,如下所示:
单元格选择和取消选择必须在代理函数的帮助下反映在indexArray中,如下所示:
现在,当为每一行调用cellforrowatinexpath函数时,检查当前索引XPath是否存在于我们的索引数组中。如果是,则该单元格已被选中,否则,该单元格处于未选中状态,并在该单元格上执行相应的任务
那么,当您不希望多个单元格被选中时,在您的表中向上滚动时,多个单元格被选中,这是一种“selectedCells”数组,其中包含选定单元格的索引。。。然后在调用方法CellForRowatineXpath时,它可以检查该索引是否在数组中,从而将单元格设置为选中。。首先想到的是:)谢谢林赛,这帮了大忙。@FilipeLouro很高兴听到:)
var indexArray : [NSIndexPath]?
self.indexArray = []
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath as IndexPath) {
cell.accessoryType = .checkmark
self.indexArray?.append(indexPath as NSIndexPath)
}
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath as IndexPath) {
cell.accessoryType = .none
if let pos = self.indexArray?.index(of: indexPath as NSIndexPath)
{
self.indexArray?.remove(at: pos)
}
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "yourCellIdentifier") as! YourTableViewCell
if (self.indexArray?.contains(indexPath as NSIndexPath))!
{
cell.accessoryType = .checkmark
}
else
{
cell.accessoryType = .none
}
return cell
}