Home > Mobile >  UILabel is not replacing previous text when view refreshed
UILabel is not replacing previous text when view refreshed

Time:10-01

I have the following function being called in a ViewController that passes in data to build a list of transactions. Each time the page refreshes, this is called and new data is shown. All is good, except for one remaining frustrating bug: when the new data is loaded the previous balancedOwedAmountLabel.text still shows the previous amount.

So on each refresh, the text is written on top of the previous text each time. This results in a jumbled mess of text each time the page is refreshed. It would be expected to have the previous text removed, and replaced with the new text that I determine.

I've tried manually setting the balancedOwedAmountLabel.text = nil to simply remove it and then reset it each time the data is retrieved and painted, but it won't work. I've also tried removing the subview entirely via balancedOwedAmountLabel.removeFromSuperview() and that doesn't seem to do the trick either.

Since I'm not using a Storyboard I can't connect IBOutlets so I'm having to resort to figuring out how to get around this problem programmatically. How do I go about solving this with the following code?

    private func buildTransactionTableView() {
        let stackview = UIStackView()
        stackview.axis = .vertical
        stackview.distribution = .fill
        stackview.spacing = 15
        self.view.insertSubview(stackview, belowSubview: (self.accountSummaryTableViewController?.view)!)
        
        stackview.translatesAutoresizingMaskIntoConstraints = false
        stackview.topAnchor.constraint(equalTo: self.transactionsTableView.bottomAnchor, constant: 0).isActive = true
        stackview.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 10).isActive = true
        stackview.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true
        stackview.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor,constant: -30).isActive = true
        
        
        // full width parent SV
        let headlineStackView = UIStackView()
        headlineStackView.axis = .horizontal
        headlineStackView.alignment = .top
        headlineStackView.distribution = .fill
        
        
        let titleLabel = UILabel(frame: CGRect(x:0,y:0,width:200,height:21))
        titleLabel.textAlignment = .left
        titleLabel.textColor = .black
        titleLabel.font = UIFont.boldSystemFont(ofSize: 14.00)
        titleLabel.text = "Transaction History"

        headlineStackView.addArrangedSubview(titleLabel)
        stackview.addArrangedSubview(headlineStackView)
        
        // SV for balance informations
        let balanceStackView = UIStackView()
        balanceStackView.axis = .horizontal
        balanceStackView.distribution = .fill
        
        headlineStackView.addArrangedSubview(balanceStackView)
        
        let balanceOwedLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
        balanceOwedLabel.textAlignment = .right
        balanceOwedLabel.textColor = .black
        balanceOwedLabel.font = balanceOwedLabel.font.withSize(14.00)
        balanceOwedLabel.text = "Balance Owed"
        
        balanceStackView.addArrangedSubview(balanceOwedLabel)
        
        let balancedOwedAmountLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 21))
        balancedOwedAmountLabel.textAlignment = .right
        balancedOwedAmountLabel.textColor = UIColor(red: 33.00/255, green: 150.00/255, blue: 243.00/255, alpha: 1.00)
        balancedOwedAmountLabel.font = UIFont.boldSystemFont(ofSize: 14.00)
        
        var amount = 0.00
        for rtp in self.userTransactions! {
            if rtp.repaymentDate == nil && rtp.rtpData.amount.value != nil {
                let value = Double((rtp.rtpData.amount.value)!)
                amount = amount   value!
            }
        }
        
        balanceStackView.addArrangedSubview(balancedOwedAmountLabel)

// This amount overwrites previous amount on screen when refreshed
// The amount is not being removed and replaced wih this new amount
        balancedOwedAmountLabel.text = " $\(String(format: "%.2f", amount))"

        let rtpTableVC = UserTransactionsTableViewController(style: .plain)
        rtpTableVC.tableView.allowsSelection = false
        rtpTableVC.tableView.separatorStyle = .none
        rtpTableVC.tableView.isScrollEnabled = true
        rtpTableVC.rtpTransactions = self.rtpTransactions
        
        stackview.addArrangedSubview(rtpTableVC.view)
        self.userTransactionsTableViewController = rtpTableVC
    }
}

CodePudding user response:

It looks like each time this function is called, you create a new stackview and add it in self.view. But I cannot see when you remove previous stackview?

One way of fixing it would be: store reference on current stackview and remove it before adding new one as:

var currentStackView: UIStackView? 
private func buildTransactionTableView() {
    currentStackView.removeFromSupperView()
    let stackview = UIStackView()
    currentStackView = stackview
    ...
}
  • Related