在Swift中使用以编程方式创建的UITableViewCell子类
我正在使用以编程方式创建的自定义单元格的表格视图,不使用IB。我在谷歌周围环顾四周并询问过NSChat,但我仍然无法让它发挥作用。它只显示默认的表视图。提前致谢!
I'm working on a table view with a custom cell that has been created programmatically, with no usage of IB. I've looked around on Google and asked around in NSChat, but I still can't get it to work. It just displays the default table view. Thanks in advance!
EventCell.swift
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))
}
}
ViewController.swift
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
}
}
首先要做一些重要的评论。
Ok first a few important comments.
首先,你每次都在不必要地创建自己的新单元格表视图请求单元格而不是重用旧单元格。你应该删除这一行:
First, you are needlessly creating your own new cell every time the table view requests a cell instead of reusing old cells. You should remove this line:
cell = EventCell(style: .Default, reuseIdentifier: cellIdendifier)
这是不必要的,因为dequeue会根据您为给定标识符注册的类,根据需要自动创建新单元格。
That is unnecessary because dequeue will automatically create new cells as needed based on the class you have registered for the given identifier.
第二,在布局代码时不应使用主屏幕边界。如果您的表视图不是整个宽度,这将分解。相反,您可以使用 self.bounds
,因此它始终相对于单元格本身。
Second, you should not be using the main screen bounds when laying out your code. This will break down if your table view is not the full width. Instead, you can use self.bounds
so it is always relative to the cell itself.
第三,你不应该调用setNeedsLayout或layoutIfNeeded,因为如果调用该方法,它已经在重新布局了所有内容。
Third, you should not be calling setNeedsLayout or layoutIfNeeded because if that method is called, it is already laying out everything again.
第四,您应该在设置表视图数据源之前注册表视图单元类,以防万一UITableView在设置数据源时开始从数据源请求内容。
Fourth, you should register your table view cell class before setting the table view data source just in case UITableView every starts requesting things from data source when the data source is set.
第五,您的两个子视图的大小为0,0因此无论如何它们都不会出现。
Fifth, two of your subviews have a size of 0,0 so they are not going to show up anyway.
如果此代码确实如此在没有崩溃的情况下运行然后你正在创建EventCells,因为你正在从 dequeueReusableCellWithIdentifier:forIndexPath
的结果强制转换。这意味着您只需要布局/显示/数据问题。
If this code is indeed running without crashing then you are creating EventCells because you are doing a forced casting from the result of the dequeueReusableCellWithIdentifier:forIndexPath
. That means you simply have a layout / display / data issue.