Ios Swift上的自定义UITableViewCell
我需要创建一个自定义UITableViewCell, 我在ViewController类中创建了一个类 我已经写了下面的代码Ios Swift上的自定义UITableViewCell,ios,uitableview,swift,Ios,Uitableview,Swift,我需要创建一个自定义UITableViewCell, 我在ViewController类中创建了一个类 我已经写了下面的代码 class FavoritesCell: UITableViewCell { var timeLabel:UILabel? override init(style: UITableViewCellStyle, reuseIdentifier: String!) { super.init(style: UITableViewCellStyle.Value1, reus
class FavoritesCell: UITableViewCell
{
var timeLabel:UILabel?
override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: UITableViewCellStyle.Value1, reuseIdentifier: reuseIdentifier)
timeLabel = UILabel(frame: CGRectMake(250, 10, 70, 20))
//how to add this time label as a subview of FavoritesCell
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
正如我在注释行中提到的,如何将timeLabel添加到UITableViewCell
谢谢在您创建了
时间标签
之后,应该可以:
self.contentView.addSubview(timeLabel!)
这会将timeLabel
添加到单元格的contentView
中,而不是单元格本身,但这通常是在表视图单元格中执行的操作
其他需要注意的事项:
- 在这里使用
很好-在调用self
之后,对象被完全初始化super.init
- 您刚刚创建了
,因此可以在timeLabel
中强制展开它addSubview
让FavoritesCell=tableView.dequeueReusableCellWithIdentifier(“您使用的Identifier”,forIndexPath:indexPath)作为MyViewController.FavoritesCell
获取cellForRowAtIndexPath中的FavoritesCell
实例。