在Swift中使用以编程方式创建的UITableViewCell子类

Jef*_*rey 28 uitableview ios swift

我正在使用以编程方式创建的自定义单元格的表格视图,不使用IB.我在谷歌周围环顾四周并询问过NSChat,但我仍然无法让它发挥作用.它只显示默认的表视图.提前致谢!

EventCell.swift

import UIKit

class EventCell: UITableViewCell {

    var eventName: UILabel = UILabel()
    var eventCity: UILabel = UILabel()
    var eventTime: UILabel = UILabel()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        self.contentView.addSubview(eventName)
        self.contentView.addSubview(eventCity)
        self.contentView.addSubview(eventTime)
    }

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

    override func layoutSubviews() {
        super.layoutSubviews()

        eventName = UILabel(frame: CGRectMake(20, 10, self.bounds.size.width - 40, 25))
        eventCity = UILabel(frame: CGRectMake(0, 0, 0, 0))
        eventTime = UILabel(frame: CGRectMake(0, 0, 0, 0))

    }

}
Run Code Online (Sandbox Code Playgroud)

ViewController.swift

class ViewController: UITableViewController, UITableViewDelegate {

    var events: Dictionary<String, [String]> = ["0": ["Monroe Family", "La Cañada", "8:30"]]

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.dataSource = self;
        tableView.delegate = self;

        tableView.registerClass(EventCell.self, forCellReuseIdentifier: "EventCell")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

    override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return UITableViewAutomaticDimension;
    }

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

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var cellIdendifier: String = "EventCell"

        var cell: EventCell = tableView.dequeueReusableCellWithIdentifier(cellIdendifier, forIndexPath: indexPath) as EventCell
        cell = EventCell(style: .Default, reuseIdentifier: cellIdendifier)

        if let i = events[String(indexPath.row)] {
            cell.eventName.text = i[0]
            cell.eventCity.text = i[1]
            cell.eventTime.text = i[2]
        }

        cell.sizeToFit()

        return cell


    }
}
Run Code Online (Sandbox Code Playgroud)

dre*_*wag 27

好的,首先是一些重要的评论.

首先,每次表视图请求单元格而不是重用旧单元格时,您都会不必要地创建自己的新单元格.你应该删除这一行:

cell = EventCell(style: .Default, reuseIdentifier: cellIdendifier)
Run Code Online (Sandbox Code Playgroud)

这是不必要的,因为dequeue将根据您为给定标识符注册的类,根据需要自动创建新单元格.

其次,在布局代码时,不应使用主屏幕边界.如果您的表视图不是整个宽度,这将分解.相反,你可以使用self.bounds它,因此它总是相对于单元本身.

第三,你不应该调用setNeedsLayout或layoutIfNeeded,因为如果调用该方法,它已经重新布局了所有内容.

第四,在设置表视图数据源之前,应该注册表视图单元类,以防万一UITableView在设置数据源时开始从数据源请求事物.

第五,你的两个子视图的大小为0,0所以它们无论如何都不会出现.

如果这段代码确实在没有崩溃的情况下运行,那么你正在创建EventCells,因为你正在从结果中强制转换dequeueReusableCellWithIdentifier:forIndexPath.这意味着您只需要布局/显示/数据问题.


Sur*_*gch 27

我有同样的问题.我在@drewag的答案中应用了这个建议,但还有一些其他问题.下面是一个简单的概念验证示例,演示如何使用以编程方式创建的UITableViewCell子类.

下面的图片应该是它的样子.黄色矩形是UILabel自定义单元格中的子视图.

在此输入图像描述

这是UITableViewCell子类.它的工作是初始化单元格及其内容,添加子视图和布局子视图.

import UIKit
class MyCustomCell: UITableViewCell {

    var myLabel = UILabel()

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        myLabel.backgroundColor = UIColor.yellowColor()
        self.contentView.addSubview(myLabel)
    }

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

    override func layoutSubviews() {
        super.layoutSubviews()

        myLabel.frame = CGRect(x: 20, y: 0, width: 70, height: 30)
    }
}
Run Code Online (Sandbox Code Playgroud)

这是视图控制器代码.

import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!

    let myArray = ["one", "two", "three", "four"]
    let cellReuseIdendifier = "cell"


    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.registerClass(MyCustomCell.self, forCellReuseIdentifier: cellReuseIdendifier)

        tableView.dataSource = self
        tableView.delegate = self
    }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier(cellReuseIdendifier, forIndexPath: indexPath) as! MyCustomCell
        cell.myLabel.text = myArray[indexPath.row]

        return cell
    }
}
Run Code Online (Sandbox Code Playgroud)

笔记:

  • 我用的是常规UIViewController而不是UITableViewController.如果你使用a UITableViewController然后删除UITableViewDelegateUITableViewDataSource协议引用,因为这些是多余的UITableViewController.你也不需要这@IBOutlet tableView条线.
  • 我在原问题中找到的主要问题是eventName = UILabel(frame: CGRectMake(...)).相反它应该是eventName.frame = CGRect(...)