TableView没有出现在我的容器中

时间:2018-05-08 13:51:54

标签: ios swift xcode uitableview

我是苹果开发的新手。我目前正在使用主细节概念构建iPad。在我的详细信息视图中,我有一些基本元素(如textfields等)和一个容器,在该容器中是一个tableView。我正在遵循本指南:https://www.youtube.com/watch?v=AaCfwqzH9SQ

我的问题是我的虚拟数据没有出现在tableview中。它只是一张空桌子。但它会打印出我在viewDidLoad方法中包含的日志消息。

Layout of the container with an embedded segue from the container to the table view

这是我的类连接到表视图:

import UIKit

class TaskTableViewController: UITableViewController {

var Array = [String]()

override func viewDidLoad() {
    super.viewDidLoad()
    print("IN THE TASKBAR")
    Array = ["ffff","ddd","sss"]

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
     self.navigationItem.rightBarButtonItem = self.editButtonItem
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 0
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return Array.count
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 


    //THIS IS WHER YOU'D PROGRAM YOUR TASK THINGS
    cell.textLabel?.text = Array[indexPath.row]

    // Configure the cell...


    return cell
}


/*
// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    // Return false if you do not want the specified item to be editable.
    return true
}
*/

/*
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        // Delete the row from the data source
        tableView.deleteRows(at: [indexPath], with: .fade)
    } else if editingStyle == .insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }    
}
*/

/*
// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

}
*/

/*
// Override to support conditional rearranging of the table view.
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
    // Return false if you do not want the item to be re-orderable.
    return true
}
*/

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

1 个答案:

答案 0 :(得分:0)

numberOfSections不应该返回zero。要在tableView中显示一些单元格,至少应该有一个单元格。

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

在您的reloadData

中插入内容后,也可以将tableView调用Array
override func viewDidLoad() {
    super.viewDidLoad()
    print("IN THE TASKBAR")
    Array = ["ffff","ddd","sss"]
    self.tableView.reloadData()
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
     self.navigationItem.rightBarButtonItem = self.editButtonItem
}

假设此方法将在按钮单击时执行。

func didClickedOnEditButton(button: UIButton){
    //First add string into your array
    Array.append("This is the newly inserted String")
    //Then ask your TableView to reload Everything
    tableView.reloadDate()
}