出乎意料地发现Nil - 使用TableView传输数据 - Swift 3

时间:2017-07-19 22:26:54

标签: swift database uitableview swift3 load

这与双关联的问题提供了一般信息,但它对我的具体问题没有帮助,因为我不太清楚如何使用该常规信息来解决我的问题。 我正在尝试让我的代码加载以前设置的用户在选择tableView单元格时输入的数据。但当我回去尝试重新选择tableView单元格时,我收到此错误:

fatal error: unexpectedly found nil when unwrapping an Optional value

我对Swift非常陌生,任何帮助都会非常感激!

这是viewController,它包含用户选择tableView单元格时应显示的详细信息:

var nametext = String()
var agetext = String()
var locationstext = String()
var intereststext = String()

@IBOutlet weak var labelGifteeName: UILabel!
@IBOutlet weak var labelGifteeAge: UILabel!
@IBOutlet weak var labelGifteeLocations: UILabel!
@IBOutlet weak var labelGifteeInterests: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    labelGifteeName.text! = nametext
    labelGifteeAge.text! = agetext
    labelGifteeLocations.text! = locationstext
    labelGifteeInterests.text! = intereststext
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "toTable" {
        let destTable = segue.destination as! TableOfGiftees
        destTable.name.append(nametext)
        destTable.save()
        save()
    }
}

func save() {
    UserDefaults.standard.set(labelGifteeName.text, forKey: "name")
    UserDefaults.standard.set(labelGifteeAge.text, forKey: "age")
    UserDefaults.standard.set(labelGifteeLocations.text, forKey: "locations")
    UserDefaults.standard.set(labelGifteeInterests.text, forKey: "interests")
    UserDefaults.standard.synchronize()
}

func load() {
    if let N = UserDefaults.standard.value(forKey: "name") as? String {
        labelGifteeName.text = N                                         //I get the error here
        labelGifteeName.reloadInputViews()
    }
    if let A = UserDefaults.standard.value(forKey: "age") as? String {
        labelGifteeAge.text = A
        labelGifteeAge.reloadInputViews()
    }
    if let L = UserDefaults.standard.value(forKey: "locations") as? String {
        labelGifteeLocations.text = L
        labelGifteeLocations.reloadInputViews()
    }
    if let I = UserDefaults.standard.value(forKey: "interests") as? String {
        labelGifteeInterests.text = I
        labelGifteeInterests.reloadInputViews()
    }
}

这是表格的viewController:

override func viewDidLoad() {
    super.viewDidLoad()
    load()
}

@IBOutlet weak var tableOfGiftees: UITableView!
var name: [String] = []



func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return name.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cellName: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cellTable")!
    cellName.textLabel?.text = name[indexPath.row]
    return cellName
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "toGifteeDetails", sender: nil)
}

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        name.remove(at: indexPath.row)

        tableView.beginUpdates()
        tableView.deleteRows(at: [indexPath], with: .automatic)
        tableView.endUpdates()
    }
    save()
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "toGifteeDetails" {
        let destDetails = segue.destination as! GifteeDetails
        destDetails.load()
    }
}

func save() {
    UserDefaults.standard.set(name, forKey: "names")
    UserDefaults.standard.synchronize()
}

func load() {
    if let loadedData = UserDefaults.standard.value(forKey: "names") as? [String] {
        name = loadedData
        tableOfGiftees.reloadData()
    }
}

0 个答案:

没有答案