Reputation: 33
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!
}
Upvotes: 0
Views: 5753
Reputation: 1
Maybe old but for those who have same problem now.
For Table view chose Content -> Static cells
Upvotes: 0
Reputation: 129
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.
Upvotes: 1
Reputation: 939
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
Upvotes: 0
Reputation: 33
I have connected both datasource and delegate even though it isn't showing up the data when i used simple tableview without customizing it is working properly, so I thought of using reload data.
Upvotes: 1