gpt4 book ai didi

ios - 将 UITableView 选择添加/删除到数组,滚动时复选标记消失 - Swift

转载 作者:行者123 更新时间:2023-11-28 09:17:05 25 4
gpt4 key购买 nike

问题 1:滚动时复选标记不断消失。

问题 2:需要帮助添加/删除具有唯一 ID 的数组以防止重复。

我正在尝试从空数组中插入/删除 cellTextLabel。我似乎找不到好的解决方案。以下是我尝试过的方法以及失败的原因。

错误的选项 1

// Error = Out of range (I understand why)
myArray.insert(cell.textLabel.text, atIndex: indexPath)

错误的选项 2

// I won't have a way to reference the array item afterwards when I need to remove it. Also, this option allows for the same string to be entered into the array multiple times, which is not good for me.
myArray.insert(cell.textLabel.text, atIndex: 0)

以下是目前为止的代码,非常感谢任何帮助。谢谢。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let row = indexPath.row
let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("items", forIndexPath: indexPath) as UITableViewCell

var myRowKey = myArray[row]
cell.textLabel.text = myRowKey

cell.accessoryType = UITableViewCellAccessoryType.None
cell.selectionStyle = UITableViewCellSelectionStyle.None


return cell
}

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


let selectedCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell!

if selectedCell.accessoryType == UITableViewCellAccessoryType.None {

selectedCell.accessoryType = UITableViewCellAccessoryType.Checkmark
}

var selectedItem = selectedCell.textLabel.text!

println(selectedItem)

}

func tableView(tableView: UITableView!, didDeselectRowAtIndexPath indexPath: NSIndexPath!) {

let deSelectedCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell!

if deSelectedCell.accessoryType == UITableViewCellAccessoryType.Checkmark {
deSelectedCell.accessoryType = UITableViewCellAccessoryType.None
}

var deSelectedItem = deSelectedCell.textLabel.text!

println(deSelectedItem)

}

最佳答案

问题 1:由于 didDeselectRowAtIndexPath 方法中的以下行,当您滚动时您的复选标记不断消失:

let deSelectedCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell!

调用 cellForRowAtIndexPath 将创建一个新单元格。它不会修改 UITableView 屏幕上当前可见的单元格。这是因为当项目在屏幕上和屏幕外滚动时,单元格会被重复使用,新数据会加载到其中。

要保留单元格的选择状态,您需要稍微升级数据模型。现在您的数据来自 myArray,它是一个 String 数组。您可以尝试如下操作:

struct Item {
var name: String // The string value you are putting in your cell
var isSelected: Bool // The selected status of the cell
}

然后你会像这样定义你的数据:

var myArray = [
Item(name: "Cell 1 value", isSelected: false),
Item(name: "Cell 2 value", isSelected: false),
...
]

而您的 tableView:didSelectRowAtIndexPath 方法看起来更像这样:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Toggle the selected state of this cell (e.g. if it was selected, it will be deselected)
items[indexPath.row].isSelected = !items[indexPath.row].isSelected

// Tell the table to reload the cells that have changed
tableView.beginUpdates()
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
tableView.endUpdates()

// Reloading that cell calls tableView:numberOfRowsInSection and refreshes that row within the tableView using the altered data model (myArray array)
}

下次您点击该行时,tableView:didSelectRowAtIndexPath 方法将再次触发并切换该单元格的选定状态。告诉该单元格重新加载将刷新屏幕上实际可见的单元格。

问题 2:在不太了解您希望保持唯一性的数据类型以及您如何以可能添加重复项的方式添加/删除数据的情况下,您可能需要查看 this answer用于从数组中删除重复的元素。一种方法是使用集合,它不会保持顺序但会确保元素只出现一次。

关于ios - 将 UITableView 选择添加/删除到数组,滚动时复选标记消失 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27079345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com