Home > database >  After tableView scrolled data puts in cells in wrong order
After tableView scrolled data puts in cells in wrong order

Time:01-25

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.

CodePudding user response:

You should use prepareForReuse() method

Simply just clear data of your labels:

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

CodePudding user response:

There are few things to check here.

  • Make sure you reset all fields before configure a new cell.
  • If you have created a cell using xib or storyboard, make sure you haven't filled labels with static text.
  • Is your guard statements passing for every item?
  • Else block for guard configures cell with a single operation, Is it handling all ui elements in cell?
  •  Tags:  
  • Related