When my code loads 5 entities are displayed on each tableview cell. When I hit a button it sorts the entity but the other 4 entities are still displayed. I have added a photo below to show you exactly what I am looking for. When the objc is pressed it sorts but all of the entities are displayed and i only want 1.
@objc func l1UPBTN(sender: UIButton){
let appD = UIApplication.shared.delegate as! AppDelegate
let context = appD.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Data")
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "Name", ascending: true)]
do {
itemName = try context.fetch(fetchRequest)
self.theScores.reloadData()
}catch {
print("Ashley Tisdale")
}
}
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
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
}
LINK TO STACKOVERFLOW QUESTION