Swift Access CollectionView单元格内容(更改内部视图的宽度)
我想有一个可变宽度的uiview作为单元格背景。这应该用作进度/状态栏 问题: 我无法从我的collectionViewController(仅从CollectionViewCellController)更改状态栏的宽度 我在LayoutSubView()中将宽度手动设置为5。然后我在setStatusBar(…)中将其更改为80,我在另一个类中调用了它。宽度保持在5:( 这是我的代码Swift Access CollectionView单元格内容(更改内部视图的宽度),swift,uicollectionview,uicollectionviewcell,uicollectionviewflowlayout,Swift,Uicollectionview,Uicollectionviewcell,Uicollectionviewflowlayout,我想有一个可变宽度的uiview作为单元格背景。这应该用作进度/状态栏 问题: 我无法从我的collectionViewController(仅从CollectionViewCellController)更改状态栏的宽度 我在LayoutSubView()中将宽度手动设置为5。然后我在setStatusBar(…)中将其更改为80,我在另一个类中调用了它。宽度保持在5:( 这是我的代码 import UIKit class MyCollectionViewCell: UICollectionV
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, y: 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
}
有人能帮我找到为什么statusbar uiview没有将其从5更改为80的问题吗
谢谢大家!