在对表格视图单元格上的一组实体进行排序时,删除除一个实体外的所有实体(Swift5)

时间:2019-05-22 04:40:06

标签: ios swift uitableview sorting

当我的代码加载时,每个表视图单元格上将显示5个实体。当我按下按钮时,它将对实体进行排序,但其他4个实体仍会显示。我在下面添加了一张照片,以向您确切显示我在寻找什么。按下objc时,它会进行排序,但是会显示所有实体,我只需要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

}

0 个答案:

没有答案