IBoutlet 在我的自定义 table 单元格中为零?
IBoutlet is nil in my custom table cell?
//一个swift文件中的自定义单元格
import UIKit
class CardTableViewCell: UITableViewCell {
@IBOutlet weak var elapsedTime: UILabel!
@IBOutlet weak var todo: UILabel!
@IBOutlet weak var startAndStop: UIView!
@IBOutlet weak var progress: UIView!
@IBOutlet weak var cardView: UIView!
}
//在另一个swift文件中自定义Table视图控制器
import UIKit
class CardFeedTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(CardTableViewCell.self, forCellReuseIdentifier: "cardCell")
tableView.delegate = self
tableView.dataSource = self
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 3
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: CardTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "cardCell", for: indexPath) as! CardTableViewCell
cell.todo.text = "study"
return cell
}
running the app triggers a error
storyboard
我不知道为什么我的 table 单元格中的所有属性都是零,当我 运行 应用程序时
There are two variants to register, but both take a parameter called
forCellReuseIdentifier, which is a string that lets you register
different kinds of table view cells. For example, you might have a
reuse identifier "DefaultCell", another one called "Heading cell",
another one "CellWithTextField", and so on. Re-using different cells
this way helps save system resources.
If you want to use register() with a Swift class, you provide a table
view cell class as its first parameter. This is useful if your cell is
defined entirely in code. As an example, this uses the default
UITableViewCell class:
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "DefaultCell")
然后您可以像这样使该单元格出列:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DefaultCell")!
return cell
}
The other option is to use register() with an Interface Builder nib
file. Nibs contain the class name to use along with their design, so
this method is more common. Here's an example
tableView.register(UINib(nibName: "yourNib", bundle: nil), forCellReuseIdentifier: "CellFromNib")
But if you're using storyboards you will find it easier to create
prototype cells and give them a reuse identifier directly inside
Interface Builder.So no need to register programmatically.
删除此行表格 viewDidLoad
tableView.register(CardTableViewCell.self, forCellReuseIdentifier: "cardCell).
//一个swift文件中的自定义单元格
import UIKit
class CardTableViewCell: UITableViewCell {
@IBOutlet weak var elapsedTime: UILabel!
@IBOutlet weak var todo: UILabel!
@IBOutlet weak var startAndStop: UIView!
@IBOutlet weak var progress: UIView!
@IBOutlet weak var cardView: UIView!
}
//在另一个swift文件中自定义Table视图控制器
import UIKit
class CardFeedTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(CardTableViewCell.self, forCellReuseIdentifier: "cardCell")
tableView.delegate = self
tableView.dataSource = self
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 3
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: CardTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: "cardCell", for: indexPath) as! CardTableViewCell
cell.todo.text = "study"
return cell
}
running the app triggers a error
storyboard
我不知道为什么我的 table 单元格中的所有属性都是零,当我 运行 应用程序时
There are two variants to register, but both take a parameter called forCellReuseIdentifier, which is a string that lets you register different kinds of table view cells. For example, you might have a reuse identifier "DefaultCell", another one called "Heading cell", another one "CellWithTextField", and so on. Re-using different cells this way helps save system resources.
If you want to use register() with a Swift class, you provide a table view cell class as its first parameter. This is useful if your cell is defined entirely in code. As an example, this uses the default UITableViewCell class:
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "DefaultCell")
然后您可以像这样使该单元格出列:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DefaultCell")!
return cell
}
The other option is to use register() with an Interface Builder nib file. Nibs contain the class name to use along with their design, so this method is more common. Here's an example
tableView.register(UINib(nibName: "yourNib", bundle: nil), forCellReuseIdentifier: "CellFromNib")
But if you're using storyboards you will find it easier to create prototype cells and give them a reuse identifier directly inside Interface Builder.So no need to register programmatically.
删除此行表格 viewDidLoad
tableView.register(CardTableViewCell.self, forCellReuseIdentifier: "cardCell).