【问题标题】:Inputing data to different struct in a different class将数据输入到不同类中的不同结构
【发布时间】:2018-12-18 05:31:18
【问题描述】:

我已经在互联网上搜索过,但我仍然不明白如何将值插入到变量中。我试图将一个值插入到一个变量中,这样我就可以将它附加到一个数组中,然后将它放在一个 tableviewcell 中。

我了解单元格的工作原理,我只是想知道如何将数据插入此变量

这是我的结构的代码

import Foundation
import UIKit

enum issueType: String {
 case major = "Major", blocker = "Blocker", minor = "Minor"
}

struct Issue {
var id: String
var tester: String
var type: issueType
var title: String
var appName: String
var desc: String
var date: Date
var bgColor: UIColor?

init(){
    id = ""
    tester = ""
    type = .minor
    title = ""
    appName = ""
    desc = ""
    date = Date()
    bgColor = UIColor.main()
}

init(item: [String:Any]){
    self.init()
    id = item["id"] as? String ?? ""
    tester = item["tester"] as? String ?? ""
    title = item["title"] as? String ?? ""
    appName = item["appName"] as? String ?? ""
    desc = item["desc"] as? String ?? ""

    if type == .major {
        bgColor = UIColor.main()
    }
    else if type == .blocker {
        bgColor = UIColor.blue
    }
    else {
        bgColor = UIColor.green
    }
}
}

这是来自不同类的 superDidLoad 中变量的代码

    override func viewDidLoad() {
    super.viewDidLoad()

    var issue1 = Issue(id: "id", tester: "tester", type: .minor, title: "title", appName: "appName", desc: "desc", date: Date())
    issue1.bgColor = UIColor.main()

    array.append(issue1)
}

【问题讨论】:

    标签: ios swift struct


    【解决方案1】:

    你的UITableViewCell 子类应该有一些Issue 类型的变量

    class YourSubclass: UITableViewCell {
    
        var issue: Issue?
        ...
    }
    

    然后在cellForRowAt TableView 数据源方法中将单元格的变量分配为来自array 的元素,索引等于indexPath.row

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        ...
        cell.issue = array[indexPath.row]
        ...
    }
    

    【讨论】:

    • 对于问题类型,我已经有了这个 var 数组:[Issue] = []
    • 我在 var issue1 行中收到“表达式类型'问题'不明确,没有更多上下文”的错误消息。
    • @Nodoodle 你从哪里得到这条消息的?
    【解决方案2】:

    将您自己的初始化程序添加到结构后,您丢失了默认的成员初始化程序。 为了避免这种使用扩展:

    struct Issue {
        var id: String
        var tester: String
        var type: issueType
        var title: String
        var appName: String
        var desc: String
        var date: Date
        var bgColor: UIColor?
    }
    
    extension Issue {
        init(){
            id = ""
            tester = ""
            type = .minor
            title = ""
            appName = ""
            desc = ""
            date = Date()
            bgColor = UIColor.main()
        }
    
        init(item: [String:Any]){
            self.init()
            id = item["id"] as? String ?? ""
            tester = item["tester"] as? String ?? ""
            title = item["title"] as? String ?? ""
            appName = item["appName"] as? String ?? ""
            desc = item["desc"] as? String ?? ""
    
            if type == .major {
                bgColor = UIColor.main()
            }
            else if type == .blocker {
                bgColor = UIColor.blue
            }
            else {
                bgColor = UIColor.green
            }
        }
    }
    

    但即使在这种情况下,您也必须通过默认初始化程序初始化所有属性,包括“bgColor”。

    var issue1 = Issue(id: "id", tester: "tester", type: .minor, title: "title", appName: "appName", desc: "desc", date: Date(), bgColor: nil)
    

    【讨论】:

      猜你喜欢
      • 2021-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-15
      • 2021-05-08
      • 1970-01-01
      • 1970-01-01
      • 2017-06-01
      相关资源
      最近更新 更多