如何解决访问 CollectionView 单元格内容更改内部视图的宽度
我想要一个 uiview 作为宽度不断变化的单元格背景。这应该用作进度/状态栏。
问题:
我无法从我的 collectionViewController(只能从 CollectionViewCellController)更改状态栏的宽度....
我在 layoutSubviews() 中手动将宽度设置为 5。然后我在 setStatusBar(...) 中将它更改为 80,我在另一个类中调用了它。宽度保持在 5 :(
这是我的代码
import UIKit
class MyCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var myLabel: UILabel!
@IBOutlet weak var statusbar: UIView!
override func layoutSubviews() {
// cell rounded section
self.layer.cornerRadius = 15.0
self.layer.masksToBounds = true
super.layoutSubviews()
statusbar.frame = CGRect(x: 0,y: 0,width: 5,height: self.frame.height)
}
}
extension MyCollectionViewCell{
func setStatusbar(completedTasks: Int,totalTasks: Int){
print(self.frame.width)
let newWidth = 80 //(self.frame.width * (CGFloat(completedTasks / totalTasks)))
statusbar.backgroundColor = .orange
statusbar.frame = CGRect(x: 0,width: newWidth,height: Int(self.frame.height))
reloadInputViews()
}
}
这就是我调用函数 setStatusbar(...) 的地方 - 我想改变宽度,但不工作:
func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// get a reference to our storyboard cell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier,for: indexPath as IndexPath) as! MyCollectionViewCell
// Use the outlet in our custom class to get a reference to the UILabel in the cell
cell.myLabel.text = self.items[indexPath.row] // The row value is the same as the index of the desired text within the array.
cell.backgroundColor = UIColor(rgb: 0xF444444)
cell.setStatusbar(completedTasks: 5,totalTasks: 25) //!!! HERE !!!
return cell
}
谁能帮我找出为什么状态栏 uiview 没有从 5 更改为 80 的问题?
谢谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。