[英]How to add images to a tableview cell through stackview
我有一個函數,該函數查找應添加到堆棧視圖中的星星數。 stackview在表格單元格內,因此顯示正確的等級,該等級是從CoreData檢索的。 但是,沒有顯示等級。
功能 ...
func setRating(entry: Entry, ratingView: UIStackView) -> UIStackView{
for _ in 0..<Int(entry.rating) {
let image = UIImage(named:"ratingFilled")
let imageView = UIImageView(image: image!)
imageView.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
NSLayoutConstraint(item: image!, attribute: .height, relatedBy: .equal, toItem: image!, attribute:.width, multiplier: 1.0, constant:0.0).isActive = true
ratingView.addArrangedSubview(imageView)
}
return ratingView
}
Tableview單元格調用...
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ //creates rzeusable cell and assigns cell items
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
let entry = entries[indexPath.row]
cell.nameLabel?.text = entry.restaurantName
cell.ratingView = setRating(entry: entry, ratingView: cell.ratingView)
return cell
}
你需要一個寬度約束添加為各處imageView
或在stackView
本身。
NSLayoutConstraint(item: image!, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 20.0).isActive = true
NSLayoutConstraint(item: image!, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 20.0).isActive = true
還要確保將stackView
軸設置為水平
stackView.axis = .horizontal
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.