gpt4 book ai didi

swift - 选择UITableView中的单个单元格即可转到UITableView显示不同的学生列表数据

转载 作者:行者123 更新时间:2023-11-30 11:02:13 24 4
gpt4 key购买 nike

已更新

我想选择每一行转到 ViewController,该 ViewController 有一个 TableView 来显示不同的学生列表数据,但对于我的代码,它将显示与我创建新学生对象相同的列表,如何解决此问题?问题是我前面没有学生数据列表,直到我单击单个类单元格转到下一个 ViewController,然后我将有一个加号按钮将学生添加到 TableView 中。

ManageViewController类:

class ManageViewController : UIViewController, UITableViewDataSource, UITableViewDelegate{

@IBOutlet weak var tableView: UITableView!

var classList = [Class]()
var selectedClass: StudentData?


override func viewDidLoad() {
super.viewDidLoad()

self.navigationItem.title = "Manage Classes"
tableView.dataSource = self

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

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


// Do any additional setup after loading the view.
}

添加类按钮操作:

@IBAction func onAddTapped(_ sender: UIBarButtonItem) {
let alert = UIAlertController(title: "Add Class", message: nil, preferredStyle: .alert)
alert.addTextField { (classListTF) in
classListTF.placeholder = "Enter Class"
}
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
let action = UIAlertAction(title: "Add", style: .default) { (_) in
guard let list = alert.textFields?.first?.text else { return }
print(list)
let subject = Class(context: PersistenceService.context)
subject.class_name = list
PersistenceService.saveContext()
self.classList.append(subject)
self.tableView.reloadData()
}
alert.addAction(action)
present(alert, animated: true)
}

类单元选择代码:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)

// Unwrap that optional
if let label = cell?.textLabel?.text {
print("Tapped \(label)")
}
self.performSegue(withIdentifier: "studentInfoView", sender: self)
}

StudentListViewController 类:

class StudentListViewController: UIViewController, UITableViewDataSource,UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!

var studentList = [StudentData]()
var selectedStudent: 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)
}

我已经更新了 ManageViewController 类和 StudentInfoViewContrller 类的附加代码。抱歉,我是一名新的 Ios 程序员。

最佳答案

您应该添加一个附加条件,以便能够根据所选行进入不同的 View ,例如 ff:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)

if indexPath.row == 0 {
self.performSegue(withIdentifier: "toViewA", sender: self)
} else {
self.performSegue(withIdentifier: "toViewA", sender: self)
}

}

关于swift - 选择UITableView中的单个单元格即可转到UITableView显示不同的学生列表数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53200500/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com