我已经阅读并看过一些有关此主题的视频,但我无法让它发挥作用。我正在尝试使用 xib 中的单元格而不是故事板。
这是我的 ViewController(其中有表视图)。
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CellTableView
cell.test.text = "A";
return cell;
}
}
这是我的 CellTableView (我拥有 Cell 的类):
import UIKit
class CellTableView: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
@IBOutlet var teste: UILabel!
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
我总是在 StoryBoard 的表视图中使用表视图单元格,但我现在想尝试 XIB 方法。
我添加了cell单元 XIB 上的标识符,用于使用 dequeueReusableCell 方法调用它。
我可能做错了什么?我试图出口数据源和delegate的表视图,但我收到错误(我认为这不是正确的步骤)。