gpt4 book ai didi

ios - 使用按钮创建/追加数组

转载 作者:行者123 更新时间:2023-11-30 12:27:39 26 4
gpt4 key购买 nike

编辑2:以为我拥有它,但我没有。我需要在按下按钮时创建数组,但我不知道如何访问每个对象的 .checkmark。我已经将“.selected”作为属性,但也无法访问它。

我有一个 UITableView,它在三个部分中显示带有复选标记的列表。当选中一行时,与该 NSObject 类关联的 bool 值从 false 更改为 true。我在 UINavigationBar 中有一个按钮,按下该按钮会显示带有“发送”和“取消”选项的警报。当用户点击“发送”时,我希望将带有复选标记的所有行添加到数组中。我认为使用该 bool 将是最好的方法,但在操作的 func 中,我无法调用与数组中每个 NSObject 关联的 bool 。我包含了我认为需要帮助我的代码部分。

编辑:我相信这就是我的类(class)设置方式,除非我误解了。其中一个类的变量是“var selected: Bool”,然后当我从该类创建一个项目时,我将其设置为 false。我有一个项目列表,在 cellForRowAt 中显示良好,但是当点击按钮时,我无法访问相同的“sortedList”。这是更多代码。也许我还缺少其他东西?

var arrayOfItemsSelected = [String]()

var sortedItems = [[Item]]()
var itemList: ItemList! {
didSet {
sortedItems = itemList.sortByBrands()
self.tableView.reloadData()
}
}

覆盖 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 让 item =sortedItems[indexPath.section][indexPath.row]

    let cell = tableView.dequeueReusableCell(withIdentifier: "UIItemViewCell", for: indexPath)
cell.textLabel?.text = "\(item.name)"
cell.detailTextLabel?.text = "\(item.style)"

cell.accessoryType = cell.isSelected ? .checkmark : .none
cell.selectionStyle = .none // to prevent cells from being "highlighted"

return cell
}

func confirmButtonPressedAction() {

// Create the alert controller
let alertController = UIAlertController(title: "List of checked items", message: stringOfSelectedItems, preferredStyle: .alert)

// Create the actions
let okAction = UIAlertAction(title: "Send", style: UIAlertActionStyle.default) {
UIAlertAction in

self.arrayOfItemsSelected.removeAll()
self.tableView.reloadData()
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) {
UIAlertAction in
}

// Add the actions
alertController.addAction(okAction)
alertController.addAction(cancelAction)

// Present the controller
self.present(alertController, animated: true, completion: nil)
}

最佳答案

您的代码要么没有对 arrayOfItemsSelected 执行任何操作,要么没有向我们展示相关部分。事实上,代码示例既不会获取也不会处理所选项目。

您可以实现 didSelectRowAt 和 didDeselectRowAt 函数来维护 Item 类中的内部标志,也可以从 indexPathsForSelectedRows 中获取选择。

如果您想完全规避 UITableView 的选择机制,则需要使用 UIItemViewCell 本身中的操作来处理单元格单击/点击(但这似乎过于复杂)。

顺便说一句,我怀疑您是否可以依赖刚刚出列的单元格中的 cell.isSelected 。此外,在 didSet 闭包中重新加载 tableview 可能会给您带来麻烦,导致性能问题、随机崩溃或无限循环。

关于ios - 使用按钮创建/追加数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43975016/

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