2017-05-25 111 views
0

我有一个函数可以查找应添加到并添加到stackview中的星星数量。堆栈视图位于表格单元格内,因此显示从CoreData检索的正确评分。但是,评级不显示。如何通过stackview将图像添加到tableview单元格

功能...

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 
} 

泰伯维细胞电话...

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

您需要的宽度约束添加到任何每个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 
+0

stackview在水平,我想我已经通过20集的ImageView框架20加入您的约束后,其仍然没有工作 –

+0

你还需要添加高度约束,检查编辑 –

+0

添加一个约束,同样的2个项目并不意味着,你需要添加约束常数,如上所示。让我知道如果这对你有帮助。 –

相关问题