ios - 更改 UICollectionView 单元格内容
问题描述
我想在我的 CollectionView 单元格的背景中有一个进度条(= 状态栏)。我想使用 UIView 来实现它(通过改变它的宽度)。这是它的外观(“状态栏”:绿色背景 UIView):
我的代码:
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()
}
}
并这样称呼它:
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)
编辑:这就是它的样子:
解决方案
推荐阅读
- javascript - 如何通过 id 单击单选按钮来激活事件?
- marklogic - MarkLogic - 通过 API 返回数据
- azure - Azure Data Lake Storage x Azure Blob 存储和 Azure 文件存储之间的区别
- python-3.x - 如何使用 while_loop 实现 TensorBoard v2 (tf.contrib.summary)?
- c - 是否有“分支”字符串格式描述符?
- reactjs - 用酶测试材料-ui文本字段中的按键
- docusignapi - 以嵌入模式签署文档并将电子邮件发送到第二个收据
- xcode10 - 在一行中突出显示 Xcode 中的整个方法范围
- python - 无法理解此函数中的关键逗号
- python - 是否有 Python 库或模块可以绘制字母并获取线的 X、Y 坐标?