我为注册页面创建了一个核心数据模型,该模型正常工作但是当我将表格视图附加到该注册页面时,我的自定义表格单元格数据没有出现。
ViewController.swift
import UIKit
import Foundation
class FifthViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
@IBOutlet weak var Table1: UITableView!
var array = ["central","gvk","forum"]
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return array.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = Table1.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
cell.label1.text = self.array[indexPath.row]
return cell
}
}
TableViewCell
import UIKit
class TableViewCell: UITableViewCell {
@IBOutlet weak var label1: UILabel!
}
答案 0 :(得分:1)
我连接了数据源和委托,即使我没有使用简单的tableview而没有自定义它正常工作时也没有显示数据,所以我想到了使用重载数据。
答案 1 :(得分:0)
试试这个,
class FifthViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
@IBOutlet weak var Table1: UITableView!
override func viewDidLoad(){
//In View did load
super.viewDidLoad()
// Register the table view cell class and its reuse id
self.tableView.register(TableViewCell.self, forCellReuseIdentifier:"cell")
Table1.delegate = self //If not set in story board or Xib
Table1.datasource = self //If not set in story board or Xib
var array = ["central","gvk","forum"]
Table1.reloadData()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return array.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
var cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? TableViewCell
if cell==nil
{
//initialize the cell here
cell = TableViewCell.init(style: .Default, reuseIdentifier: "cell")
}
cell.label1.text = self.array[indexPath.row]
return cell
}
}
在故事板或xib中自定义表视图单元的设计中检查标识符之前,应该在行索引路径的单元格中与上面相同 注意:这只是伪代码,可能包含错误
您还可以查看完整教程here
答案 2 :(得分:0)
试试这个会对你有所帮助:
class FifthViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
@IBOutlet weak var Table1: UITableView!
var array = ["central","gvk","forum"]
override func viewDidLoad() {
super.viewDidLoad()
Table1.delegate = self
Table1.dataSource = self
// Do any additional setup after loading the view.
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return array.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
cell.label1.text = self.array[indexPath.row]
return cell
}
}
你忘了打电话:
Table1.datasource = self
和Table1.delegate = self
不调用datasource tableview不会显示任何数据。
答案 3 :(得分:0)
也许老了,但是对于那些现在同样有问题的人。
对于“表”视图,选择了Content -> Static cells