Search code examples
iosswiftuitableviewtableview

After tableView scrolled data puts in cells in wrong order


in my View:

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  let cell = tableView.dequeueReusableCell(withIdentifier: "TransactionTableCell", for: indexPath) as! TransactionTableCell
            let newItem = getTransactionsInSection(section: sectionHeader[indexPath.section])[indexPath.row]
            cell.configure(item: newItem)
}

in my TransactionTableCell

    func configure(item: TransactionModel) {
        
        guard let withdrawalBonuses = item.withdrawalBonuses,
              withdrawalBonuses < 0,
              let accruedBonuses = item.accruedBonuses,
              accruedBonuses > 0 else {
                  configureWithOneOperation(item)//shows one line of operation
                  return
              }

//show 2 lines of operations

        firstOperationAmountLabel.text = "+\(Int(accruedBonuses))"
        secondOperationAmountLabel.text = "\(Int(withdrawalBonuses))"
}

When I scroll the cell , second operation line is appears in wrong cells where its shouldn't be, even If I reload my table , that also has this problem.


Solution

  • You should use prepareForReuse() method

    Simply just clear data of your labels:

    override func prepareForReuse() {
        super.prepareForReuse()
        
        firstOperationAmountLabel.text = nil
        secondOperationAmountLabel.text = nil
    }