skip to Main Content

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.

2

Answers


  1. 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?
    Login or Signup to reply.
  2. You should use prepareForReuse() method

    Simply just clear data of your labels:

    override func prepareForReuse() {
        super.prepareForReuse()
        
        firstOperationAmountLabel.text = nil
        secondOperationAmountLabel.text = nil
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search