[英]TableViewController with custom cells in swift
我在Swift中使用自定义TableViewCell
遇到问题。 我正在尝试在TableViewCell
中显示自定义TableView
但是效果不佳。 我在屏幕上看到一个灰色方块...我在Internet上找到了一些建议禁用自动布局的答案。 我这样做了,但是现在显示了我的单元格,但是单元格的高度混乱了。 如您所见,本应具有红色背景的单元格显示在3个单元格的高度上,而背景仅显示在1个单元格上。 单元格的高度和行高度都设置为150px。
With Auto-Layout: Without Auto-Layout:
这是单元格代码:
class CharacterDescription : UITableViewCell {
@IBOutlet var imageProfile: UIImageView!
@IBOutlet var name: UILabel!
@IBOutlet var hp: UILabel!
@IBOutlet var damages: UILabel!
@IBOutlet var range: UILabel!
@IBOutlet var mp: UILabel!
@IBOutlet var critChances: UILabel!
@IBOutlet var critModifier: UILabel!
required init(coder aDecoder: NSCoder!) {
super.init(coder: aDecoder)
}
override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: UITableViewCellStyle.Value1, reuseIdentifier: reuseIdentifier)
}
}
和TableViewController
代码:
class CharacterDescriptionsViewController : UITableViewController, UITableViewDataSource, UITableViewDelegate {
var userProfile: UserProfile!
required init(coder aDecoder: NSCoder!) {
super.init(coder: aDecoder)
}
override init() {
super.init()
}
override func viewDidLoad() {
var nibName = UINib(nibName: "CharacterDescription", bundle:nil)
self.tableView.registerNib(nibName, forCellReuseIdentifier: "CharacterDescription")
}
override func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
return 1
}
override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
return userProfile.characters!.count
}
override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
var cell = self.tableView.dequeueReusableCellWithIdentifier("CharacterDescription", forIndexPath: indexPath) as CharacterDescription
//!TODO: Init of cell
return cell
}
}
您必须重写TableView.heightForRowAtIndexPath
才能返回自定义单元格的高度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.