更改tableView中所有单元格的所有对象的布尔状态

更改tableView中所有单元格的所有对象的布尔状态

本文介绍了更改tableView中所有单元格的所有对象的布尔状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

tableView中的每个单元格都有各种属性:

Each cell in my tableView has various properties:

class: Item
// properties
    var name: String
    var photo: UIImage?
    var category: String
    var instructions: String
    var completed = false

(一切都已初始化)

我的tableView数组是:

my tableView array is:

items = [Item]()

当Item.complete为true时,单元格将有一个选中标记的附件,否则会有.none

When Item.complete is true, the cell will have a checkmark accessory, else it'll have .none

我想使用UIButton(在单独的VC中)为每个单元格设置Item.complete = false

I want to use a UIButton (in a separate VC) to make Item.complete = false for every cell in the tableView, thus removing all of the checkmarks and essentially resetting the data.

我的项目:项目数组是可变的,因为用户可以选择添加或删除任何内容数组中的行。

My items: Item array is variable, in that the user has the option to add or delete any row in the array.

我希望UIButton(位于单独的VC中的单独选项卡中)选择到tableView并进行更改。

I want the UIButton (located on a separate VC in a separate tab) to segue to the tableView and make the changes.

如何使用UIButton将每个cell的.complete属性更改为false,以便可以删除每个cell上的复选标记?

How can I change each cells .complete property to equal false with a UIButton so that I can remove the checkmark on every cell?

推荐答案

您还可以使用 .map 函数。

您可以直接在游乐场页面:

You cam run this directly in a Playground page:

class Item: NSObject {
    var name: String = ""
    var completed: Bool = false
}

// declare items array
var items = [Item]()

// fill with 20 Items,
// setting .completed for every-other one to true
for n in 0...19 {
    let newItem = Item()
    newItem.name = "item-\(n)"
    newItem.completed = n % 2 == 0
    items.append(newItem)
}

// print out the items in the array
print("Original values")
for item in items {
    print(item.name, item.completed)
}
print()

// -- this is the .map function
// set .completed to false for every item in the array
items = items.map {
    (item: Item) -> Item in
    item.completed = false
    return item
}
// -- this is the .map function

// print out the items in the array
print("Modified values")
for item in items {
    print(item.name, item.completed)
}

,然后当然在表上调用 .reloadData()

and then, of course, call .reloadData() on your table.

这篇关于更改tableView中所有单元格的所有对象的布尔状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 12:03