[英]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.