我和斯威夫特有些问题。
需要使tableview和每个单元格中都有包含文本的图像。
这就是我到目前为止所做的:
第一个问题:
标签应该是自动高度,现在它打断了字符串。。
第二个问题:
图像也需要自动高度,并取决于标签高度。
第三个问题:
行需要根据其内部自动调整高度。
表视图控制器代码:
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension;
tableView.estimatedRowHeight = 44.0;
tableView.tableFooterView = UIView(frame: CGRectZero)
tableView.registerNib(UINib(nibName: "QuoteTableViewCell", bundle: nil), forCellReuseIdentifier: "QuoteCell")
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return results.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("QuoteCell", forIndexPath: indexPath) as! QuoteTableViewCell
cell.item = results[indexPath.row]
return cell
}
手机号码:
class QuoteTableViewCell: UITableViewCell {
var item: Quote! {
didSet {
setupCell()
}
}
@IBOutlet weak var imageField: UIView!
@IBOutlet weak var textField: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
func setupCell() {
self.imageField.backgroundColor = UIColor(patternImage: UIImage(named: "Bg")!)
textField.text = item.text
}
}
最佳答案
要使标签的高度与其内容相适应,请在情节提要中将其行号设置为0。
如果你想在旁边有一个图像,为什么不把两个控件放在一个水平的StackView中呢?
要使tableView行高适应单元格内容(即标签高度),只需将其row height设置为automatic:
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 90.0;
关于swift - 带有自动高度标签的Swift tableview单元格自动高度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35986361/