想知道我如何以编程方式在swift 3.0中继承UITableViewCell?

时间:2016-11-03 05:16:15

标签: ios swift uitableview

正如标题所示,我试图以编程方式在swift 3.0中自定义/子类化UITableViewCell类,并将自定义单元格显示在表格中。

我声明了以下UITableViewCell的子类:

import Foundation
import UIKit

class TableViewCellCustom: UITableViewCell {
    var myLabel: UILabel!
    var myButton: UIButton!

    let screenSize = UIScreen.main.bounds

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.contentView.backgroundColor = UIColor.blue

        myLabel = UILabel()
        myLabel.frame = CGRect(x: 180, y: 20.0, width: 50.0, height: 20.0)
        myLabel.textColor = UIColor.black
        myLabel.textAlignment = .center
        contentView.addSubview(myLabel)

    }
}

这应该允许我做一个简单的情况,即在我的指定的单元格中的特定位置具有标签的自定义单元格。关于如何将它集成到我委托UITableView的ViewController中,我有点困惑。

目前我的ViewController看起来像这样:

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var tableView: UITableView  =   UITableView()

    var items: [String] = ["Party at Dougs", "Pillage at the Village", "Meow party at sams"]

    let screenSize: CGRect = UIScreen.main.bounds

    var appTitle: UIView! = nil
    var appTitleText: UILabel! = nil

    override func viewDidLoad() {
        super.viewDidLoad()
        let screenWidth = screenSize.width
        let screenHeight = screenSize.height

        tableView.frame         =   CGRect(origin: CGPoint(x: 0,y :screenHeight*0.12), size: CGSize(width: screenWidth, height: screenHeight*0.88))
        tableView.delegate      =   self
        tableView.dataSource    =   self

        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")

        self.view.addSubview(tableView)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "td")


        cell.textLabel?.text = items[indexPath.row]


        self.tableView.rowHeight = screenSize.height * 0.15
        return cell
    }

}

我认为我的问题的核心是不确定如何将我的UITableViewCell的子类连接到我的TableView。我怎样才能做到这一点?感谢。

1 个答案:

答案 0 :(得分:1)

对于以编程方式创建的自定义单元格,不使用xib,这是您当前的情况,您可以使用以下代码 -

而不是

tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")

使用此行

tableView.register(TableViewCellCustom.self, forCellReuseIdentifier: "cell")

也代替行

let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "td")

使用此行

let cell = TableViewCellCustom(style: UITableViewCellStyle.default, reuseIdentifier: "td")

使用xib创建自定义单元格时。如果您有自定义单元格UITableViewDataSource,请在cellForAtIndexPath xib中使用以下代码。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("td") as? TableViewCellCustom
    if cell == nil {
         tableView.registerNib(UINib(nibName: "TableViewCellCustom", bundle: nil), forCellReuseIdentifier: "td")
         cell = tableView.dequeueReusableCellWithIdentifier("td") as? TableViewCellCustom
     }
        cell.textLabel?.text = items[indexPath.row]


        self.tableView.rowHeight = screenSize.height * 0.15
        return cell
    }