来自 Xib 的单元格 Swift 3
Cell from Xib with Swift 3
我已经阅读并看过一些关于这个主题的视频,但我无法让它发挥作用。我正在尝试从 xib 而不是情节提要中的单元格。
这是我的 ViewController(我有 Table 视图)。
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;
}
}
这是我的手机Table视图(我有手机的 class):
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 上的 Table 视图中使用 Table View Cell,但我现在想尝试 XIB 方法。
我在单元 XIB 上添加了 cell 标识符,以使用 dequeueReusableCell 方法调用它。
我可能做错了什么?我试图 Outlet dataSource 和 Table 视图的 delegate 但我得到了一个错误(我认为它不正确步骤)。
您需要注册您的 nib 对象。
在viewDidLoad()
中:
let nib = UINib(nibName: "nameOfYourNibFile", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "yourIdentifier")
您还需要return节数:
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
First of all, You have to register your nib object.
yourTable?.register(UINib(nibName: "yourCustomCell", bundle: nil), forCellReuseIdentifier: "cellIdentifier")
我已经阅读并看过一些关于这个主题的视频,但我无法让它发挥作用。我正在尝试从 xib 而不是情节提要中的单元格。
这是我的 ViewController(我有 Table 视图)。
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;
}
}
这是我的手机Table视图(我有手机的 class):
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 上的 Table 视图中使用 Table View Cell,但我现在想尝试 XIB 方法。
我在单元 XIB 上添加了 cell 标识符,以使用 dequeueReusableCell 方法调用它。
我可能做错了什么?我试图 Outlet dataSource 和 Table 视图的 delegate 但我得到了一个错误(我认为它不正确步骤)。
您需要注册您的 nib 对象。
在viewDidLoad()
中:
let nib = UINib(nibName: "nameOfYourNibFile", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "yourIdentifier")
您还需要return节数:
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
First of all, You have to register your nib object.
yourTable?.register(UINib(nibName: "yourCustomCell", bundle: nil), forCellReuseIdentifier: "cellIdentifier")