Change what is displayed in cell for row in tableview cell

I want to hit a button and change what is printed in a tableview cell after view did load. When my code loads 5 items are printed in each tableview cell. I want to hit a button and when hit it should only print 2 attributes and change what is printed before the attribute. The attributes come from core data.

                  var set = 0 

@objc func l1UPBTN(sender: UIButton){
set == 1
theScores.reloadData()
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) → UITableViewCell {
let title = itemName[indexPath.row]
let cell = theScores.dequeueReusableCell(withIdentifier: “MyCell”, for : indexPath)
cell.textLabel!.numberOfLines = 2
cell.selectionStyle = .default

if set == 1 {
    let attr1 = title.value(forKey: "atBATS") as? String
    let attr5 = title.value(forKey: "positon") as? String
    let text = [attr1,"  eeeee :"," we :",attr5].flatMap { $0 }.reduce("", +)
    cell.textLabel?.text = "\(text)"
    cell.textLabel?.textAlignment = .center
    cell.layoutMargins = UIEdgeInsets.zero
}

if set == 0 {
    let attr1 = title.value(forKey: "atBATS") as? String
    let attr3 = title.value(forKey: "age") as? String
    let attr4 = title.value(forKey: "weight") as? String
    let attr2 = title.value(forKey: "hits") as? String
    let attr5 = title.value(forKey: "positon") as? String
    let text = [attr1,"  Weight :",attr3,"\n","Height : ",attr2," Age : ",attr4," Position :",attr5].flatMap { $0 }.reduce("", +)
    cell.textLabel?.text = "\(text)"
}

cell.textLabel?.textAlignment = .center
cell.layoutMargins = UIEdgeInsets.zero
cell.preservesSuperviewLayoutMargins = false
cell.separatorInset = UIEdgeInsets.zero
cell.layoutMargins = UIEdgeInsets.zero

return cell

}

Isn’t that supposed to be set = 1?

This topic was automatically closed after 166 days. New replies are no longer allowed.