Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

ios - Change UICollectionView Cell Content

I want to have a progress bar (= statusbar) in the background of my CollectionView Cells. I want to implement this using a UIView (by changing its width). This is how it looks ("statusbar": The Green background UIView):

screenshot

My Code:

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()
    }
}

And call it like this:

 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
    }

PROBELM:

It does not change the width of the UIView (stays at width = 5, should be width = 80)

EDIT: This is what it looks like:

screen


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...