簡體   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