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

时间:2018-11-29 00:41:56

标签: swift uitableview uiviewcontroller

在上一个TableView中选择“班级”单元后,我试图通过单击按钮添加学生姓名,我面临的问题是,例如,我有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)")
}

}

1 个答案:

答案 0 :(得分:0)

我没有看到您为名单学生过滤类的任何代码行。 我认为您的班级StudentData缺少变量“班级”以显示该学生所在的班级。 然后对于tableView,您有2个选择:

  • 一次获取所有学生,然后创建showArray以通过以下方式将所有学生留在1个班级:

    self.showArray = studentList.filter {$ 0.class ==“ A”}

  • 使用CoreData NSPredicate按类获取数据

    fetchRequest.predicate = NSPredicate(格式:“ class =%@”,“ A”)