我正在做一個程式化的 Swift 應用程式,但在使用 tableView.dequeueReusableCell 時找不到向單元格添加樣式:.subtitle的方法。
在下面的代碼中,我希望能夠將樣式設定為 .subtitle:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
...
return cell
}
我嘗試添加以下內容,但它不能與出隊結合使用:
var cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cell")
添加此內容的正確方法是什么?
uj5u.com熱心網友回復:
子類UITableViewCell,并覆寫init(style:reuseIdentifier:):
class MyTableViewCell: UITableViewCell {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
}
}
然后,在表視圖中注冊這個類:
tableView.register(MyTableViewCell.self, forCellReuseIdentifier: "cellIdentifier")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/368493.html
上一篇:將空nchar轉換為數值
