0

I gotta create a UITableView Cell with a "line" passing through. Do you guys know how to do it? It will be a price tag, so I want to say something like: "Old price and now with 50% OFF". So, the "old price" should be crossed.

UI Code

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "topSoldCell", for: indexPath) as! TableViewCell

    let content = self.dataTopSold[indexPath.item]
    cell.labelNomeTopSell.text = content.nome
    cell.imageViewTopSell.setImage(url: content.urlImagem, placeholder: "")
    cell.labelPrecoDe.text = "R$ \(content.precoDe)"
    cell.labelPrecoPor.text = "R$ 119.99"
    return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    performSegue(withIdentifier: "segueId", sender:self.dataTopSold[indexPath.row])

    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

         if segue.identifier == "segueId" {

        let des = segue.destination as! TelaDetalheProdutos

        des.stringNomeeDesc = sender as! Top10
    }
}

2 Answers2

0

As Old price is static content and also at first place in label. So you can take line image and put on Label at first place so it will look like on Old price . Now you can hide/show image according to your need.

If you have to give underline to some text in label then simply put that string in TextEdit. Edit their as you want. And copy to label text by setting label style Plain to Attributed.

Hope so this work for you

Manish Mahajan
  • 2,062
  • 1
  • 13
  • 19
0

In your case

It will be a price tag, so I want to say something like: "Old price and now with 50% OFF". So, the "old price" should be crossed.

You can using attributed string to support it.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "topSoldCell", for: indexPath) as! TableViewCell
    let content = self.dataTopSold[indexPath.item]
    cell.labelNomeTopSell.text = content.nome
    cell.imageViewTopSell.setImage(url: content.urlImagem, placeholder: "")
    let oldPrice = "R$ \(content.precoDe)"
    let promotionString = oldPrice + " and now with 50% OFF"
    let attributedStr = NSMutableAttributedString(string: promotionString)
    let crossAttr = [NSAttributedStringKey.strikethroughStyle: NSUnderlineStyle.styleSingle.rawValue]
    attributedStr.addAttributes(crossAttr, range: NSMakeRange(0, oldPrice.count))
    cell.labelPrecoDe.text = attributedStr
    cell.labelPrecoPor.text = "R$ 119.99"
    return cell
}
Quoc Nguyen
  • 2,839
  • 6
  • 23
  • 28