我有一个函数可以找到应该添加到堆栈视图中并添加到堆栈视图的星号数。 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
}
答案 0 :(得分:0)
您需要为每个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