【发布时间】:2016-03-14 11:30:54
【问题描述】:
我对 swift 有一些问题。 需要制作 tableview 并在每个单元格中放置带有文本的图像。
这是我到目前为止所做的:
标签应该是自动高度,现在它会破坏字符串..
第二个问题:
图像也需要自动高度,并且取决于标签高度。
第三题:
行需要根据其内部自动高度。
TableViewController 代码:
@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
}
}
【问题讨论】:
-
你的 UILabel 和 UIImageView 有什么限制?
-
我在图像视图上使用常量在顶部和底部设置间距,然后使用标签常量在该视图上设置间距。顺便说一句,我不使用 imageview,我使用 UIView,但它是一样的..我更新了我有常量的图像。
-
您是否尝试在 UILabel 和 UI(Image)View 之间添加“等高”?我想如果你这样做应该没问题。应用文本后,还要在 UILabel 上调用 .sizeToFit() 。那么它应该更好地包裹文本的高度。
-
@TedHuinink 这行得通,但我失去了顶部和底部的填充,我需要很少的行之间的空白。解决方案?
-
您是否在单元格的顶部和底部之间为 UITableviewCell 的直接子项(在这种情况下我认为是 UIView)添加了约束?因为它应该在正确设置时自动换行。
标签: swift tableviewcell uitableview