I have created a core data model for a sign up page which worked properly but when I attach a table view to that sign up page my custom table cell data is not appearing.
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!
}
Try this it will help you:
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
}
}
You forgot to call: Table1.datasource = self
and Table1.delegate = self
Without calling datasource tableview will not show any data.
我已经连接了数据源和委托,即使当我使用简单的 tableview 而不自定义它时它没有显示数据,它工作正常,所以我想使用重新加载数据。
Try this,
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
}
}
And before all of this check for identifier in the design of Custom table view cell in story board or xib It should be same as above in cell for row index path
Note : This is just pseudo code, might contain error
And also you can check full tutorial, here
Maybe old but for those who have same problem now.
For Table view chose Content -> Static cells
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.