[英]Custom table view cell: IBOutlet label is nil
考慮以下簡單的視圖控制器:
class ViewController: UIViewController, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var items = ["One", "Two", "Three"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "customCell")
self.tableView.dataSource = self
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("customCell") as CustomTableViewCell
cell.titleLabel!.text = self.items[indexPath.row]
return cell
}
}
和自定義單元格視圖:
class CustomTableViewCell: UITableViewCell {
@IBOutlet weak var titleLabel: UILabel!
}
此代碼導致以下錯誤。
致命錯誤:在展開可選值時意外發現 nil
titleLabel
為 nil — 不清楚原因。 設置UITableViewCell
的默認屬性(如textLabel
)工作得很好。
我正在為自定義單元格使用筆尖。
標簽和表格視圖都正確連接到它們的IBOutlet
。
原型單元格和自定義 nib 視圖都被標記為具有CustomTableViewCell
類。
我是 iOS 開發的新手,我是否遺漏了一些明顯的東西?
首先,您使用 nib 文件將自定義單元格加載到表格中。 如果您是 Swift/Cocoa 的新手,這可能會更令人頭疼。 我暫時將所有內容移至情節提要。 不要使用 nib 文件單擊,而是轉到 Storyboard,單擊您的UITableView
並確保 TableView 的內容設置為Dyanamic Prototypes
:
接下來,單擊原型單元格(表格視圖中的唯一一個)並將類設置為CustomTableViewCell
並將其重用標識符設置為customCell
:
接下來,將標簽添加到原型單元格並將其鏈接到CustomTableViewCell
類中的 IBOutlet。 只要您在情節提要中設置了重用標識符,您就不需要注冊您的customCell
。 刪除這一行:
self.tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "customCell")
它應該運行。
嘗試這個
import UIKit
class ViewController: UIViewController, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var items = ["One", "Two", "Three"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerNib(UINib(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: "customCell")// CustomTableViewCell.self, forCellReuseIdentifier: "customCell")
self.tableView.dataSource = self
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as CustomTableViewCell
cell.titleLabel!.text = self.items[indexPath.row]
return cell
}
}
像這樣注冊你的筆尖:
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "PickerCell", bundle: bundle)
collectionViewPicker.register(nib, forCellWithReuseIdentifier: "cell")
您應該使用dequeueReusableCellWithIdentifier:forIndexPath
將單元格出列。
如果您使用情節提要創建單元格,則不需要注冊該類以進行重用,因為情節提要為您執行此操作,如果您設置了重用標識符。
請確保您在 viewdidload 中注冊您的 nib(自定義單元格)時沒有犯任何錯誤。 Nib name 和reuseIdentifiers 應該沒有錯。
CustomTableViewCell中的Outlet必須具有強引用。
更換
@IBOutlet weak var titleLabel: UILabel!
同
@IBOutlet var titleLabel: UILabel!
對我有用!
編輯
let challenge = self.challenges![indexPath.row]
let title = challenge["name"]
if title != nil {
cell.titleLabel.text = title
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.