Swift - 以编程方式添加 UITableViewCell 无法正常工作
Swift - Add UITableViewCell programmatically don't work properly
我有这个代码:
class Cell:UITableViewCell {
var fullName:UILabel! = UILabel()
var userImage:UIImage! = UIImage()
override func awakeFromNib() {
super.awakeFromNib()
println("wake")
fullName.setTranslatesAutoresizingMaskIntoConstraints(false)
contentView.addSubview(fullName)
let imageView = UIImageView(image: userImage)
contentView.addSubview(imageView)
var viewsDict = [
"fullName" : fullName,
"userImage" : userImage
]
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[userImage(10)]", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[fullName]-[userImage(10)]-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
}
}
viewDidLoad:
var tableView = UITableView()
tableView.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height-64)
tableView.delegate = self
tableView.dataSource = self
var cell = Cell.self
tableView.registerClass(cell, forCellReuseIdentifier: "cell")
self.view.addSubview(tableView)
cellForRowAtIndexpath:
let cell:Cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as Cell
cell.fullName.text = testarray.objectAtIndex(indexPath.row) as? String
return cell
我执行代码时看不到单元格中的标签,甚至 class 单元格中的 println("wake") 也没有执行。
awakeFromNib
仅在从笔尖或故事板加载对象时调用。您没有这样做,因为 class 已注册。
您需要将该代码移动到 initWithStyle:reuseIdentifier
方法,如 UITableView 文档中提到的 dequeue 方法:
If you registered a class for the specified identifier and a new cell must be created, this method initializes the cell by calling its initWithStyle:reuseIdentifier:
method. For nib-based cells, this method loads the cell object from the provided nib file
您的自定义 UITableViewCell
应该像下面的代码:
class ListTableViewCell: UITableViewCell {
var lblTitle = UILabel()
override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
lblTitle = UILabel(frame: CGRectZero)
lblTitle.font = UIFont(name: "HelveticaNeue-Light", size: 18.0)
lblTitle.textAlignment = NSTextAlignment.Right
lblTitle.numberOfLines = 1
lblTitle.backgroundColor = UIColor.clearColor()
lblTitle.textColor = UIColor(red: 130/255, green: 137/255, blue: 141/255, alpha: 1.0)
self.contentView.addSubview(self.lblTitle)
// 130 137 141
self.separatorInset = UIEdgeInsets(top: 0, left: 100, bottom: 0, right: -100)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
var marginX = CGFloat(30.0)
var marginY = CGFloat(0.0)
var width = CGFloat(320)
var height = self.contentView.frame.size.height
self.lblTitle.frame = CGRectMake(marginX, marginY, width, height)
}
}
这就是如何在 UITableView
数据源的 cellForRowAtIndexPath
方法中创建单元格。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:ListTableViewCell! = tableView.dequeueReusableCellWithIdentifier(self.cellIdentifier) as? ListTableViewCell
if (cell == nil) {
cell = ListTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: self.cellIdentifier)
}
cell.lblTitle.text = self.listItems[indexPath.row]
return cell
}
我有这个代码:
class Cell:UITableViewCell {
var fullName:UILabel! = UILabel()
var userImage:UIImage! = UIImage()
override func awakeFromNib() {
super.awakeFromNib()
println("wake")
fullName.setTranslatesAutoresizingMaskIntoConstraints(false)
contentView.addSubview(fullName)
let imageView = UIImageView(image: userImage)
contentView.addSubview(imageView)
var viewsDict = [
"fullName" : fullName,
"userImage" : userImage
]
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[userImage(10)]", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[fullName]-[userImage(10)]-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDict))
}
}
viewDidLoad:
var tableView = UITableView()
tableView.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height-64)
tableView.delegate = self
tableView.dataSource = self
var cell = Cell.self
tableView.registerClass(cell, forCellReuseIdentifier: "cell")
self.view.addSubview(tableView)
cellForRowAtIndexpath:
let cell:Cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as Cell
cell.fullName.text = testarray.objectAtIndex(indexPath.row) as? String
return cell
我执行代码时看不到单元格中的标签,甚至 class 单元格中的 println("wake") 也没有执行。
awakeFromNib
仅在从笔尖或故事板加载对象时调用。您没有这样做,因为 class 已注册。
您需要将该代码移动到 initWithStyle:reuseIdentifier
方法,如 UITableView 文档中提到的 dequeue 方法:
If you registered a class for the specified identifier and a new cell must be created, this method initializes the cell by calling its
initWithStyle:reuseIdentifier:
method. For nib-based cells, this method loads the cell object from the provided nib file
您的自定义 UITableViewCell
应该像下面的代码:
class ListTableViewCell: UITableViewCell {
var lblTitle = UILabel()
override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
lblTitle = UILabel(frame: CGRectZero)
lblTitle.font = UIFont(name: "HelveticaNeue-Light", size: 18.0)
lblTitle.textAlignment = NSTextAlignment.Right
lblTitle.numberOfLines = 1
lblTitle.backgroundColor = UIColor.clearColor()
lblTitle.textColor = UIColor(red: 130/255, green: 137/255, blue: 141/255, alpha: 1.0)
self.contentView.addSubview(self.lblTitle)
// 130 137 141
self.separatorInset = UIEdgeInsets(top: 0, left: 100, bottom: 0, right: -100)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
var marginX = CGFloat(30.0)
var marginY = CGFloat(0.0)
var width = CGFloat(320)
var height = self.contentView.frame.size.height
self.lblTitle.frame = CGRectMake(marginX, marginY, width, height)
}
}
这就是如何在 UITableView
数据源的 cellForRowAtIndexPath
方法中创建单元格。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:ListTableViewCell! = tableView.dequeueReusableCellWithIdentifier(self.cellIdentifier) as? ListTableViewCell
if (cell == nil) {
cell = ListTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: self.cellIdentifier)
}
cell.lblTitle.text = self.listItems[indexPath.row]
return cell
}