我已经在互联网上搜索了很多解决此问题的方法,但是我不知道。我正在尝试在表格视图中创建自定义单元格。
我制作了一个CustomCell.swift类,以在自定义单元中配置所需的标签,通过情节提要(表视图中的第一个原型单元)创建了该标签,并将其与标识符链接到cellForRowAtIndexPath方法
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cellIdentifier = "huisCell"
var cell: CustomCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? CustomCell
if cell == nil {
cell = CustomCell(style: UITableViewCellStyle.Value1, reuseIdentifier: cellIdentifier)
}
cell!.huisAdresLabel.text = "123"
cell!.huisDetailLabel.text = "456"
return cell
}
我的CustomCell.swift代码是这样的:
class CustomCell: UITableViewCell {
@IBOutlet var huisAdresLabel: UILabel
@IBOutlet var huisDetailLabel: UILabel
}
现在这是非常基础的,但是我只希望它能正常工作,因为我可以用更多属性扩展单元格并对其进行更好的样式设置。
通过DropBox拍摄图片,因为我需要10个信誉才能正确记录我的问题:)
https://www.dropbox.com/sh/5v9jb6cqp80knze/AAD5-yPR8-KoStQddkqKIbcUa
我希望有人能解释我在做什么错。
编辑:
为了弄清楚一些事情,在尝试创建自定义单元之前,我将其与基本单元一起使用,左侧带有一个标签。但是,当我尝试为表格视图设置样式并创建自定义单元格时,它将无法正常工作。
另外,在测试不同的解决方案时,我遇到了一个问题,即CustomCell.swift中的两个标签为nil。即使当我进行自定义初始化并确实
self.huisAdresLabel = UILabel()
它仍然是零。在我向您展示的代码中,它打印出以下内容:
<UILabel: 0xb2aadc0; frame = (0 -21; 42 21); text = '123'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0xb2aa3a0>>
我通过重写以下功能来解决此问题,手动设置单元格的高度:
override func tableView(tableView:UITableView!, heightForRowAtIndexPath indexPath:NSIndexPath)->CGFloat
{
return 44
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句