【问题标题】:Create and display different data after perform a cell selection in UITableView在 UITableView 中执行单元格选择后创建和显示不同的数据
【发布时间】:2018-11-29 00:41:55
【问题描述】:

我在上一个TableView中选择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)")
}

}

【问题讨论】:

    标签: swift uitableview uiviewcontroller


    【解决方案1】:

    我没有看到您为列表学生过滤课程的任何代码行。 我认为您的班级 StudentData 缺少变量“ class ”以显示学生所在的班级。 然后对于您的 tableView,您有 2 个选项:

    • 一次获取所有学生,然后创建 showArray 以将所有学生保持在 1 个班级:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多