【问题标题】:Use button to create/append array使用按钮创建/追加数组
【发布时间】:2017-10-13 23:23:09
【问题描述】:

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

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

编辑:我相信这就是我设置班级的方式,除非我有误解。类的变量之一是“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)
}

【问题讨论】:

    标签: ios arrays swift uitableview swift3


    【解决方案1】:

    您必须为每个数组对象添加布尔检查。下面的链接

    对于目标 C:

    @[
      @{
         @"Name":@"abc",
         @"Status":[NSNumber numberWithBool:YES]
       }
    ]
    

    对于斯威夫特:

    [
      [
        "Name": "abc",
        "Status": true
      ]
    ]
    

    【讨论】:

    • 问题被标记为 swift
    • 编辑顶帖,提供更多信息和回复
    【解决方案2】:

    如果你在 Item 类上有一个属性 checked,你可以试试:

    let itemsChecked = sortedItems.filter { $0.checked == true }

    【讨论】:

      【解决方案3】:

      您必须为每个数组对象添加布尔检查,例如 myModel。

      [
      "Name": "abc",
      "Status": true
      

      ]

      选中一行时,

       myModel.Status = myModel.Status ? false : true
      

      myModel 的默认状态 = false

      那么“你可以调用与数组中每个NSObject关联的bool”

      【讨论】:

        【解决方案4】:

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

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

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

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

        【讨论】:

        • 项目有一个 Bool .selected 作为属性。当单元格被选中时,它设置为 true,未选中设置为 false。我很确定我做的那部分是对的。现在一切似乎都正常工作,即使我不完全理解为什么。但是我创建了一个新数组来填充按下我的发送按钮时为真的项目。现在我担心你的最后一段。我没有在 tableview 中填充足够的单元格,以至于需要 iOS 将它们中的任何一个出列。我得看看会发生什么......
        猜你喜欢
        • 2016-01-28
        • 2018-09-25
        • 2020-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多