Arrays 创建类的所有对象的数组

Arrays 创建类的所有对象的数组,arrays,swift,class,tableview,Arrays,Swift,Class,Tableview,是否有一种方法可以创建所有颜色对象的数组?因此,每次添加新颜色时,它也会自动添加到阵列中 class Colours { var colourName: String var colourShades: [String?] init(colourName: String, colourShades: [String?]) { self.colourName = colourName self.colourShades = colourS

是否有一种方法可以创建所有颜色对象的数组?因此,每次添加新颜色时,它也会自动添加到阵列中

class Colours {
    var colourName: String
    var colourShades: [String?]


    init(colourName: String, colourShades: [String?]) {
        self.colourName = colourName
        self.colourShades = colourShades
    }


}

var red = Colours(colourName: "Red", colourShades: ["Crimson", "Cherry", "Rose"])


var blue = Colours(colourName: "blue", colourShades:["Ice", "Baby", "Royal"])
为了给一些背景知识,我正在尝试开发一个IOS应用程序,其中包括一个颜色表。然后,当用户点击一种颜色时,它会将他们带到另一张有该颜色阴影的桌子上

我想要颜色数组,这样我就可以自动填充表中的行,然后当用户添加新颜色时,它会自动添加新行

是否有一种方法可以创建所有颜色对象的数组?所以 每次添加新颜色时,它也会自动添加 到阵列

class Colours {
    var colourName: String
    var colourShades: [String?]


    init(colourName: String, colourShades: [String?]) {
        self.colourName = colourName
        self.colourShades = colourShades
    }


}

var red = Colours(colourName: "Red", colourShades: ["Crimson", "Cherry", "Rose"])


var blue = Colours(colourName: "blue", colourShades:["Ice", "Baby", "Royal"])
您可以在类中声明一个数组并像这样追加

lazy var colorList = {
  Colours()
}()
我想要颜色的数组,这样我就可以自动填充 表的行,然后当用户添加新颜色时,它将 自动添加新行

在添加颜色时调用上述方法后,请刷新表格以在表格行中显示新的颜色信息

为了提供一些背景信息,我正在尝试为IOS开发一个应用程序 包括一张颜色表。然后,当用户点击一种颜色时 我会把他们带到另一张有这种颜色的桌子上

现在,在tableView
cellforrowatinexpath
方法中,您只需要访问上面的
colorlist
数组,获取
colorname
并显示在表上


当用户点击单元格时,然后在
DidSelecterOwatineXPath
方法中加载另一个类,并将上述
colorList
数组信息传递给该类,为了显示颜色阴影,请从
colorList
数组访问
ColorShades
信息。

使用静态数组在
colors
中定义以保存已创建的所有颜色。您可以从应用程序中的任何位置以
colors.allcolors
的形式访问此阵列

创建一个名为
colorWatcher
的协议,并在
colors
上为一个类创建一个
static
委托,该类将在添加颜色时收到通知

让您的tableView实现
colorWatcher
,并将自身添加为
代理。然后,当添加颜色时,将在
TableViewController
中调用方法
newcolorAdded
,您可以重新加载数据

此外,我建议只使用
[String]
而不是使用可选颜色。空数组表示没有阴影

protocol ColourWatcher: class {
    func newColourAdded(colour: Colour)
}

class Colours {
    static var allColours: [Colours] = []
    static weak var delegate: ColourWatcher?

    var colourName: String
    var colourShades: [String]

    init(colourName: String, colourShades: [String]) {
        self.colourName = colourName
        self.colourShades = colourShades
        Colours.allColours.append(self)
        Colours.delegate?.newColourAdded(colour: self)
    }
}

class MyTableViewController: UIViewController, ColourWatcher {

    func viewDidLoad() {
        super.viewDidLoad()
        Colours.delegate = self
    }

    func newColourAdded(colour: Colour) {
        // reload table view or just insert a new row
        // using the passed in colour
    }
}

在我看来,您希望在整个应用程序中保留一个状态

解决问题的最佳方法是创建一个实例

例如

稍后,您应该添加

ColoursSharedModel.shared.choosenColors.append(self)
在您的颜色初始化中。

1)添加您自己的通知名称

extension NSNotification.Name {

    // 1
    static let NewColourAdded = NSNotification.Name(rawValue: "NewColourAdded")
}

2) 将对象池的访问修饰符(静态变量
all
)设置为
private(Set)

3) 将初始化的颜色添加到对象池

4) 张贴通知

class Colours {

    // 2
    private(set) static var all: [Colours] = []

    var name: String
    var shades: [String]

    init(name: String, shades: [String]) {
        self.name = name
        self.shades = shades

        // 3
        Colours.all.append(self)

        // 4
        NotificationCenter.default.post(
            name: .NewColourAdded, 
            object: self
        )
    }
}

使用
谢谢你,这帮了大忙。我的tableView函数中出现以下错误:func tableView(u tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{let cell=tableView.dequeueReusableCell(带标识符:“cell”,for:indexath)cell.textlab?.text=colors.allcolors[indexath.row]返回单元格}错误为无法将“colors”类型的值分配给“String”类型。而且它只在我在newColourAdded func中将颜色更改为颜色时起作用,对吗?
color
vs.
colors
只是一个打字错误。虽然您的类只表示一种颜色,但它可能应该命名为
color
。请尝试
cell.textlab?.text=colors.allcolors[indexPath.row].colorname
。这样就消除了错误,以后我会记住的。我现在没有错误,但是表显示为空,即使我已经向类添加了一个对象。我已经在TableController中添加了行数和行内容,使用常规格式没有错误。有什么我遗漏的吗?很抱歉,这让我很痛苦,我对这件事还不太熟悉,我正努力想办法摆脱它!您应该返回
colors.allcolors.count
for
numberOfRowsInSection
。这就是我所拥有的,但仍然是空的。不管我做什么,桌子都是空的。我一直在尝试我能想到的一切,但似乎没有任何区别:/无论如何,谢谢你的帮助!
class Colours {

    // 2
    private(set) static var all: [Colours] = []

    var name: String
    var shades: [String]

    init(name: String, shades: [String]) {
        self.name = name
        self.shades = shades

        // 3
        Colours.all.append(self)

        // 4
        NotificationCenter.default.post(
            name: .NewColourAdded, 
            object: self
        )
    }
}
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        NotificationCenter.default.addObserver(
            self, 
            selector: #selector(self.onNewColourAdded), 
            name: .NewColourAdded, 
            object: nil
        )
    }

    func onNewColourAdded(notification: Notification) {
        guard let newColours = notification.object as? Colours else { 
            return 
        }

        // Do something
    }
}