gpt4 book ai didi

ios - 类 Checklist ViewController 没有初始化程序?

转载 作者:行者123 更新时间:2023-11-30 14:10:50 25 4
gpt4 key购买 nike

当我尝试运行我的应用程序时,它突然显示类没有初始化程序的错误。告诉我他们是否有任何代码问题或变量声明问题。任何人都可以告诉我此错误背后的原因。

import UIKit
class ChecklistViewController: UITableViewController {

var row0item: ChecklistItem
var row1item: ChecklistItem
var row2item: ChecklistItem
var row3item: ChecklistItem
var row4item: ChecklistItem

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) - > Int

{
return 5

}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) - > UITableViewCell

{
let cell = tableView.dequeueReusableCellWithIdentifier("ChecklistItem")

let label = cell!.viewWithTag(1000) as!UILabel



if indexPath.row == 0 {

label.text = row0item.text

} else if indexPath.row == 1 {

label.text = row1item.text

} else if indexPath.row == 2 {

label.text = row2item.text

} else if indexPath.row == 3 {

label.text = row3item.text

} else if indexPath.row == 4 {

label.text = row4item.text

}

configureCheckmarkForCell(cell!, indexPath: indexPath)

return cell!
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {


if let cell = tableView.cellForRowAtIndexPath(indexPath) {


if indexPath.row == 0 {
row0item.checked = !row0item.checked

} else if indexPath.row == 1 {
row1item.checked = !row1item.checked

} else if indexPath.row == 2 {
row2item.checked = !row2item.checked

} else if indexPath.row == 3 {
row3item.checked = !row3item.checked
} else if indexPath.row == 4 {
row4item.checked = !row4item.checked

}
configureCheckmarkForCell(cell, indexPath: indexPath)

}
tableView.deselectRowAtIndexPath(indexPath, animated: true)

}

override func viewDidLoad() {
super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()

}

func configureCheckmarkForCell(cell: UITableViewCell, indexPath: NSIndexPath) {

var isChecked = false

if indexPath.row == 0 {

isChecked = row0item.checked
} else if indexPath.row == 1 {

isChecked = row1item.checked
} else if indexPath.row == 2 {

isChecked = row2item.checked
} else if indexPath.row == 3 {

isChecked = row3item.checked
} else if indexPath.row == 4 {

isChecked = row4item.checked
}

if isChecked {

cell.accessoryType = .Checkmark
} else {

cell.accessoryType = .None
}
}

}

最佳答案

是的,您的变量声明有问题。如果您将变量声明为非可选(就像您所做的那样),则必须在覆盖 init() 中初始化它们。如果您将它们声明为可选,则不必初始化它们:

var row0item: ChecklistItem?
var row1item: ChecklistItem?
var row2item: ChecklistItem?
var row3item: ChecklistItem?
var row4item: ChecklistItem?

访问行项目:

row0item!.aPropertyOrFunction

关于ios - 类 Checklist ViewController 没有初始化程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31787362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com