【问题标题】:Select a single cell in a UITableView to go to a UITableView to display different student lists data在 UITableView 中选择单个单元格以转到 UITableView 以显示不同的学生列表数据
【发布时间】:2018-11-08 01:57:51
【问题描述】:

更新

我想选择每一行以转到具有 TableView 以显示不同学生列表数据的 ViewController,但是对于我的代码,它将显示与我创建新学生对象相同的列表,我该如何解决这个问题? 问题是我没有学生数据列表,直到我单击单个班级单元格转到下一个 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 程序员。

【问题讨论】:

  • 无论选择什么cell,您都在对标识符为studentInfoViewview 执行segue,因此您将始终看到相同的view
  • 是的,ViewController是一样的,但是显示的数据不一样,不管选择哪个cell,还是ViewController不同更好?
  • 是的,如果布局相同,那么它必须是单个ViewController,但基于所选单元格的数据不同。
  • 是的,但问题是当我将数据传递到此视图时,其他人也在传递相同的数据。
  • 其他谁?我们不知道你是如何传递数据的,所以不能说太多。

标签: swift uitableview segue viewcontroller


【解决方案1】:

您应该添加一个附加条件,以便能够根据所选行转到不同的视图,例如 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)
    }

}

【讨论】:

  • 感谢您的代码。问题是我不知道我有多少行,因为我手动创建它们,我的应用程序是从一个按钮创建一个班级列表,然后单击一个班级单元格来创建这个班级的新学生列表。
  • 我明白了,所以主要问题并不是真正根据选择的行呈现不同的视图,而是根据选择的行呈现具有不同数据的视图。在那种情况下,DV 的回答就足够了。
【解决方案2】:

您可以使用prepare(for segue: UIStoryboardSegue, sender: Any?)delegate 将学生数据传递给您的下一个视图UIViewController

在您的 UIViewController 中,其中列出了所有学生的 UITableView,声明了一个变量以使学生远离表,以及您的 studentList 变量。

var selectedStudent: StudentData?

之后,在UITableView委托方法didSelectRowAt中,设置selectedStudent的值。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    // Make sure that the selected inexPath.row is within the bounds of the studentsList array.
    if indexPath.row < studentList.count {
        selectedStudent = studentList[indexPath.row]
        performSegue(withIdentifier: "studentInfoView", sender: self)
    }
}

然后,在同一个 UIViewController 中,分配显示学生详细信息的 UIViewControllerstudent 属性。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "studentInfoView" {
        // Replace 'StudentInfoViewController' with your detail UIViewController class name.
        let destinationVC = segue.destination as? StudentInfoViewController
        destinationVC?.student = selectedStudent
    }
}

确保您在 StudentInfoViewController(或者,无论您如何称呼它)中声明了 student 属性。

class StudentInfoViewController: UIViewController {

    var student: StudentData?

    // .......
    // .......
}

然后,在viewDidLoad()StudentInfoViewController中的任何其他设置方法中,您可以在屏幕上设置学生的详细信息。

【讨论】:

  • 由于StudentInfoViewController类中的selectedStudent变量,而UITableView委托方法didSelectRowAt在ManageViewController类中,请问如何使用该变量?
  • 正如我在答案中提到的,selectedStudent 不应该在 StudentInfoViewController 中;它应该在具有 UITableView 委托方法的控制器中。根据您的评论,它应该在 ManageViewController 中。
  • 我很困惑,因为最后你在 StudentInfoViewController: UIViewContrller 中说 var student: StudentData?,我按照你的建议做了同样的事情,在 ManageViewController 中创建了 selectedStudent 变量,然后实现了 func prepare(for segue: UIStoryboardSegue, sender: Any?),但它给了我错误 Cannot assign value of type 'StudentData?'输入“[StudentData]”
  • 请分享您遇到此错误的位置,并附上屏幕截图。
  • @DV studentList 也在 StudentListViewController 类中,那么如何在 ManageViewContrller 类中使用呢?
猜你喜欢
  • 2012-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-10
相关资源
最近更新 更多