ios - TableView 没有出现在我的容器中
问题描述
我对苹果开发完全陌生。我目前正在使用主细节概念构建 iPad。在我的详细视图中,我有一些基本元素(例如文本字段等)以及一个容器,并且在该容器中是一个 tableView。我正在关注本指南:https ://www.youtube.com/watch?v=AaCfwqzH9SQ
我的问题是我的虚拟数据没有出现在表格视图中。这只是一张空桌子。但它会打印出我包含在 viewDidLoad 方法中的日志消息。
这是我连接到表格视图的课程:
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.
}
*/
}
解决方案
numberOfSections
不应该返回zero
。tableView
要在您的至少一个部分中显示某些单元格,应该可用。
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()
}
推荐阅读
- javascript - 使用不可序列化参数的 Puppeteer exposeFunction
- html - CSS 在 li::before 中设置 SVG 的高度和宽度
- javascript - 在 Node.js 中循环遍历多维数组
- java - Java / Ebeans SQLException
- python - 通过从文件中读取值来绘制 CDF:matplotlib
- angularjs - 我在滥用 ng-init 吗?
- javascript - Vue - 排序和切片表导致无限循环错误
- ruby-on-rails - Rails:强参数不起作用,禁止属性
- html - CSS网格布局,简单的响应式设计
- python - 三个或更多熊猫数据框列的所有可能组合的乘法运算