I have a function that finds the number of stars that should be added to and adds to the stackview. The stackview is inside a table cell and thus displays the correct rating, which is retrieved from CoreData. However, the rating is not displaying.
the function ...
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 cell call...
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
}
You need to add a width constraint to either each imageView
or the the stackView
itself.
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
Also make sure to set stackView
axis to horizontal
stackView.axis = .horizontal
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.