Gianluca Caliendo
Gianluca Caliendo

Reputation: 31

How to make a Progress View in a TableView?

I'm trying to make a Progress View on a TableView.

I succeeeded to create progressview, but if animation is true bar disappears, and if is false bar is full... i can't understand why.

This is my code:

here is my appending method:

if  formazione == formazione {
        guestInfoList.append("Formazione: \(formazione)%")
    }

and here the full dequeue function:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var countdowncell = tableView.dequeueReusableCell(withIdentifier: "countdowncell", for: indexPath)

    //countdown
    if indexPath.row == 9{
        countdowncell.textLabel?.text = calcolaCountdown()
    }else{
        countdowncell.textLabel?.text = guestInfoList[indexPath.row]
    }

    //Creazione Progress View
    if indexPath.row == 12 {
        cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")
        let progressView = UIProgressView(progressViewStyle: .default)
        //setting height of progressView
        let transform = CGAffineTransform(scaleX: cell.frame.size.width, y: cell.frame.size.height)
        progressView.transform = transform
        cell.contentView.addSubview(progressView)
    //**//inserisci valore formazione
        progressView.setProgress(Float(formazione), animated: false)
    }

    return countdowncell
}

Ps: and if i want to put the progress View on the right Side of the cell?


Edit

Finally i made it! Thank you matt!!

        if indexPath.row == 12 {
        var cell = tableView.dequeueReusableCell(withIdentifier: "formprogresscell", for: indexPath)
        let progressView = UIProgressView(progressViewStyle: .default)
        //setting height of progressView
        progressView.frame = CGRect(x: 230, y: 20, width: 130, height: 130)
        progressView.progress += 0.5
        progressView.rightAnchor.accessibilityActivate()
        //**//inserisci valore formazione
        progressView.setProgress(Float(formazione), animated: true)
        progressView.progressTintColor = UIColor.red
        cell.textLabel?.text = "Formazione: \(formvalue)%"
        cell.contentView.addSubview(progressView)
    }

Upvotes: 1

Views: 2195

Answers (1)

matt
matt

Reputation: 534893

The problem is what you do with the progress view after creating it:

let progressView = UIProgressView(progressViewStyle: .default)

You are failing to give the progressView any frame. Therefore it doesn't have one. In particular, it has no width. Therefore it is invisible.

Giving it a frame will solve both your problems: you'll give the view a width, and you'll give it an origin so you can put it where you want inside the content view. (But in fact you should be using autolayout, not the frame.)

Upvotes: 1

Related Questions