首页 > 解决方案 > TableView 没有出现在我的容器中

问题描述

我对苹果开发完全陌生。我目前正在使用主细节概念构建 iPad。在我的详细视图中,我有一些基本元素(例如文本字段等)以及一个容器,并且在该容器中是一个 tableView。我正在关注本指南:https ://www.youtube.com/watch?v=AaCfwqzH9SQ

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

具有从容器到表格视图的嵌入式 segue 的容器布局

这是我连接到表格视图的课程:

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.
}
*/

}

标签: iosswiftxcodeuitableview

解决方案


numberOfSections不应该返回zerotableView要在您的至少一个部分中显示某些单元格,应该可用。

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

reloadData您将某些内容插入到您的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()
}

推荐阅读