swift中tableViewcell的配置有兩種方式
- 需要注冊cell,注冊cell帶forIndexPath
var tableView : UITableView!
static let cellId = "cellIdl"
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
tableView = UITableView.init(frame: self.view.bounds, style: .plain)
tableView.delegate = self
tableView.dataSource = self
view.addSubview(tableView)
//cell注冊
tableView.register(marketCell.self, forCellReuseIdentifier: MarketViewController.cellId)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: MarketViewController.cellId, for: indexPath) as! marketCell
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 50
}
- 不用注冊cell
var tableView : UITableView!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
tableView = UITableView.init(frame: view.bounds, style: .plain)
tableView.delegate = self
tableView.dataSource = self
view.addSubview(tableView)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cellIdl")
if cell == nil {
cell = UITableViewCell.init(style: .default, reuseIdentifier: "cellIdl")
}
return cell!
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 50
}
注冊的方法會自動幫我們完成分配新單元格這個工作 而非注冊則需要我們自己手動創(chuàng)建新的單元格
1.如果注冊,當標識符為identifier 的Cell隊列中沒有可復用的cell時,系統(tǒng)會自動創(chuàng)建該類型的cell。
2.如果沒有注冊,需要判定是否有無可復用的cell,并手動新建一個cell。
注冊后就不用管cell是否需要新建了
tip: OC中沒有注意這個點。。。。好像oc中也有這個規(guī)則,奇奇怪怪