源码

首页 » 归档 » 源码 » 如何在我的viewController中访问我的自定义UITableViewCell的方法?

如何在我的viewController中访问我的自定义UITableViewCell的方法?

custom cell 

import UIKit

class CustomCell1: UITableViewCell {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func configure() {
print("Cell got configured")
}
}

View Controller :

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
let cellId = "cell"

override func viewDidLoad() {
super.viewDidLoad()
setupTableView()
}

func setupTableView() {
let table = UITableView()
table.delegate = self
table.dataSource = self
view.addSubview(table)
table.register(CustomCell1.self, forCellReuseIdentifier: cellId)
table.translatesAutoresizingMaskIntoConstraints = false
table.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
table.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
table.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
table.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}

func numberOfSections(in tableView: UITableView) -> Int {
return 4
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
switch section {
case 0:
return "Value 1 Cell Style | Section: /(section) - "
default:
return "Default cell header title"
}
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
switch indexPath.section {
case 0:
cell = UITableViewCell.init(style: .value1, reuseIdentifier: cellId) as! CustomCell1
cell.configure(...) // This method isn't working
cell.textLabel?.text = "Title Label /(indexPath.row) - "
cell.detailTextLabel?.text = "Detail label"
default:
cell = UITableViewCell.init(style: .default, reuseIdentifier: cellId)
cell.textLabel?.text = "Title Label /(indexPath.row) - "
cell.detailTextLabel?.text = "Detail Label"
}

return cell
}
}

后来我用了这种方法解决了问题:

其实使用dequeueReusableCell不是正确的方法,

使用不同的cell identifier 

例如

let cell1Id = "cell1"        
let defaultCellId = "defaultCell"

table.register(CustomCell1.self, forCellReuseIdentifier: cell1Id)
table.register(UITableViewCell.self, forCellReuseIdentifier: defaultCellId)

让dequeueReusableCell函数来创建和重用cell:

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    switch indexPath.section {
    case 0:
        let cell = tableView.dequeueReusableCell(withIdentifier: cell1Id, for: indexPath) as! CustomCell1
        cell.configure()
        cell.textLabel?.text = "Title Label /(indexPath.row) - "
        cell.detailTextLabel?.text = "Detail label"
        return cell
    default:
        let cell = tableView.dequeueReusableCell(withIdentifier: defaultCellId, for: indexPath)
        cell.textLabel?.text = "Title Label /(indexPath.row) - "
        cell.detailTextLabel?.text = "Detail Label"
        return cell
    }
}
(0)

本文由 投稿者 创作,文章地址:https://blog.isoyu.com/archives/ruhezaiwodeviewcontrollerzhongfangwenwodezidingyiuitableviewcelldefangfa.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:8 月 22, 2019 at 02:34 上午

热评文章