繁体   English   中英

Swift 3-自定义单元格未显示?

[英]Swift 3 - Custom cells not showing?

在试图弄清楚为什么没有显示海关单元格几个小时后,我遇到了一些麻烦。 使用MVC模式:

模型模型

struct studentProperties {
   var _title : String!
   var _forename : String!
   var _surname : String!
}

class MainModel {

   // Singleton instances
   static let modelInstance = MainModel()
   static var structInstance = studentProperties()

   // Array of structs
   var studentArray: [studentProperties] = []

   // MARK: - Initialization
   private init() {}

   func studentInput(title: String, forename: String, surname: String) {
       MainModel.structInstance._title = title
       MainModel.structInstance._forename = forename
       MainModel.structInstance._surname = surname
    }
}

CreateStudentController.swift

我不会显示整个文件,但是此代码位于保存按钮的内部-用户输入数据,并且该数据被追加到模型中的数组中

 @IBAction private func saveNewStudentButton(_ sender: UIBarButtonItem) {

    // Put student data into the struct properties in model
    MainModel.modelInstance.studentInput(title: studentTitle.text!, forename: studentForename.text!, surname: studentSurname.text!)

    // Append the user input
    MainModel.modelInstance.studentArray.append(MainModel.structInstance)

    dismiss(animated: true, completion: nil)
    performSegue(withIdentifier: "returnToStudentsList", sender: sender)
 }

MainStudentController.swift

class MainStudentController: UIViewController, UITableViewDelegate, UITableViewDataSource {

   //MARK: - @IBOutlets
   @IBOutlet weak var myTableView: UITableView!

   //MARK: - @IBActions
   @IBAction func addStudentButton(_ sender: UIBarButtonItem) {

       dismiss(animated: true, completion: nil)

       performSegue(withIdentifier: "createStudentSegue", sender: sender)
   }

   override func viewDidLoad() {
       super.viewDidLoad()

       myTableView.delegate = self
       myTableView.dataSource = self

       myTableView.reloadData()
   }

   // MARK: - Table view data source
   func numberOfSections(in tableView: UITableView) -> Int {

       return 1
   }

   func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

       print(MainModel.modelInstance.studentArray.count)
       return MainModel.modelInstance.studentArray.count
   }

   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

       let cell: MainCell = self.myTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MainCell

       cell.studentTitle?.text = MainModel.modelInstance.studentArray[0]._title
       cell.studentForename?.text = MainModel.modelInstance.studentArray[0]._forename
       cell.studentSurname?.text = MainModel.modelInstance.studentArray[0]._surname

       return cell
   }

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

       myTableView.deselectRow(at: indexPath, animated: true)
   }
}

还有另一个文件-它是UITableViewCell的文件,但实际上只有我的自定义单元的出口。

问题

我当前遇到的问题是(我认为)围绕此方法?

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    print(MainModel.modelInstance.studentArray.count)
    return MainModel.modelInstance.studentArray.count
}

用户输入数据->它会追加到该数组中->因此,现在我使用数组中的那些元素来表示行数。

可悲的是,它当前没有显示任何行? 我可以添加多个元素,但仍然不会显示任何行。 但是,当我将同一行打印出来时,它表明里面有元素(取决于我添加新数据的次数)。 我对为什么不添加行感到有些困惑?

我不确定这是我遗漏的东西还是代码不正确的东西。 如果有人能看到我哪里出问题了,我将不胜感激。

希望这可以解释我的情况。

谢谢

根据此代码,您的问题仅在于表视图本身未重新加载。 您唯一的重载调用是在viewDidLoad ,因此仅在您的视图首次加载时才被加载(该方法仅被调用一次)。 您可以通过将myTableView.reloadData()viewDidLoad移到viewWillAppearviewDidAppear轻松解决此问题。 那应该就是您要做的。

像这样:

override func viewWillAppear() {
       super.viewWillAppear()
       myTableView.reloadData()
   }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM