jeudi 27 août 2020

While scrolling tableview not loading properly with if else condition in swift

I am showing all orders in tableview.. if i select row then in next viewcontroller i can cancel the order...

here in the canceled order cell i need to show "Cancelled" label and i need to strike the order name.. here cell adding correctly and if i cancel the order then cancel label coming and order name also striked out..

but the problem is if once i scroll up and down then some random cells also some showing strike mark in the order name? why? whats wrong in my if else condition in cellForRowAt

coreect cancel order

enter image description here

random cell showing strike mark like this

random cells showing strike mark like this

my code in here whats wrong in cellForRowAt if else condition:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    let cell: EventsTableViewCell = tableView.dequeueReusableCell(withIdentifier: "EventsTableViewCell") as! EventsTableViewCell
    let order     = orderList!.orders[indexPath.row]
            
    if order.status == "Cancelled" {
        cell.cancelLbl.text = " \(order.status!) "
        let attributeString: NSMutableAttributedString =  NSMutableAttributedString(string:order.orderName!)
        attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length))
        cell.nameLbl.attributedText = attributeString
    }
            
    else{
        cell.cancelLbl.text = ""

        if order.isAllDayOrer == true{
            
            cell.cancelLbl.text = ""

            cell.tohardcodeLabel.isHidden = true
            cell.fromHardcodeLabel.text = "order Date"
            
            cell.startLbl.text = order.orderDate

            cell.nameLbl.text  = order.orderName
            cell.AddrLbl.text  = order.orderAddress
            cell.orderownerNameLabel.text = order.orderOwnerName as? String

        }
        else {
           
            cell.cancelLbl.text = ""

            cell.tohardcodeLabel.isHidden = false
            cell.fromHardcodeLabel.text = "From Date"

            cell.startLbl.text = order.orderDate + " " + order.orderTime
            
            cell.nameLbl.text  = order.orderName
            cell.AddrLbl.text  = order.orderAddress
            cell.orderownerNameLabel.text = order.orderOwnerName as? String
        }

    }

    return cell
}

please help me with code.

Aucun commentaire:

Enregistrer un commentaire