【发布时间】:2017-03-09 10:19:50
【问题描述】:
我已经阅读并看过一些关于这个主题的视频,但我无法让它发挥作用。我正在尝试从 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 方法。
我在单元格 XIB 上添加了 单元格 标识符,以便使用 dequeueReusableCell 方法调用它。
我可能做错了什么?我尝试输出 dataSource 和 Table View 的 delegate,但出现错误(我认为这不是正确的步骤)。
【问题讨论】:
标签: swift uitableview xib