swift - 在 UITableView 中执行单元格选择后创建并显示不同的数据

标签 swift uitableview uiviewcontroller

我尝试在上一个 TableView 中选择类(class)单元格后单击按钮来添加学生姓名,我面临的问题是,例如,我有三个类(class),A、B、C 类。然后我选择A 类创建学生 X,但是当我返回 B 类或 C 类时,我还在这些类中看到学生 X。我意识到我对所有类使用相同的数据,但我无法解决这个问题。

import UIKit
import CoreData

class StudentListViewController: UIViewController, UITableViewDataSource,UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!

var studentList = [StudentData]()

var student: StudentData?

override func viewDidLoad() {
    super.viewDidLoad()

    let fetchRequest: NSFetchRequest<StudentData> = StudentData.fetchRequest()

    do {
        let studentList = try PersistenceService.context.fetch(fetchRequest)
        self.studentList = studentList
        self.tableView.reloadData()
    } catch {}

}

@IBAction func addStudentTapped(_ sender: UIBarButtonItem) {
    let alert = UIAlertController(title: "Add Student", message: nil, preferredStyle: .alert)
    alert.addTextField { (studentListTF) in
        studentListTF.placeholder = "Enter name"
    }
    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
    let action = UIAlertAction(title: "Add", style: .default) { (_) in
        guard let student = alert.textFields?.first?.text else { return }
        print(student)
        let person = StudentData(context: PersistenceService.context)
        person.student_name = student
        PersistenceService.saveContext()
        self.studentList.append(person)
        self.tableView.reloadData()
    }
    alert.addAction(action)
    present(alert,animated: true)
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return studentList.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let stuCell = tableView.dequeueReusableCell(withIdentifier: "studentCell", for: indexPath)

    stuCell.textLabel?.text = studentList[indexPath.row].student_name

    return stuCell
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    guard editingStyle == .delete else {return}
    let person = studentList[indexPath.row]
    studentList.remove(at: indexPath.row)
    tableView.deleteRows(at: [indexPath], with: .fade)
    PersistenceService.context.delete(person)
    PersistenceService.saveContext()

    let fetchRequest: NSFetchRequest<StudentData> = StudentData.fetchRequest()
    do {
        let studentList = try PersistenceService.context.fetch(fetchRequest)
        self.studentList = studentList
    } catch {}
    self.tableView.reloadData()
    print("Delete \(person)")
}

}

最佳答案

我没有看到您为列表学生过滤类(class)的任何代码行。 我认为你的类(class) StudentData 缺少变量“class”来显示学生所在的类(class)。 然后对于你的 tableView,你有 2 个选择:

  • 一次获取所有学生,然后创建 showArray 以将所有学生保留在 1 个类(class):

    self.showArray = StudentList.filter{$0.class == "A"}

  • 使用 CoreData NSPredicate 按类获取数据:

    fetchRequest.predicate = NSPredicate(format: "class = %@", "A")

关于swift - 在 UITableView 中执行单元格选择后创建并显示不同的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53530218/

相关文章:

SwiftUI:防止 Image() 将 View 矩形扩展到屏幕边界之外

ios - iOS:通过表格 View 选择获取“id”值和项目

ios - 如何通过按下单独 View 中的按钮来增加标签?

ios - 如何在 iPhone 上实现自定义键盘弹出

swift - 使用 alamofire 的 https 请求失败

swift - 创建一个 swifty-json 字符串数组

具有关联类型和影子类型删除的 Swift 协议(protocol)

ios - UITableView 内容大小何时使用行插入/删除动画更新

swift - 文本与应删除的文本重叠

ios - MFMailComposeViewController 删除 rootViewcontroller