Reputation: 409
I am new to Swift, and iOS development in general. I am attempting to create a custom UITableViewCell. I have created the cell in my main storyboard on top of a UITableView that is inside a UIViewController. When I loaded one of the default cells, I was able to populate it with data. However, now that I am using a custom cell, I cannot get any data to appear in the table. I have gone through all kinds of tutorials and questions posted on the internet, but I can't figure out why it is not working. Any help would be appreciated.
Here is my code for the UIViewController that the tableview resides in.
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var tableView: UITableView!
var name = ["Thinh", "Nhut", "Truong", "Tuyet", "Hoai", "Hoang", "NSHipster", "iOS Developer Tips", "Jameson Quave", "Natasha The Robot", "Coding Explorer", "That Thing In Swift", "Andrew Bancroft", "iAchieved.it", "Airspeed Velocity"]
var ngaysinh = ["10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991", "10/10/1991"]
var namsinh = ["1991", "1991", "1991", "1991", "1991", "1991", "1991", "1991", "1991", "1991", "1991", "1991", "1991", "1991", "1991"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return name.count
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell
cell.hoten.text = name [indexPath.row]
cell.ngaysinh.text = ngaysinh [indexPath.row]
cell.namsinh.text = namsinh [indexPath.row]
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let row = indexPath.row
print(name[row])
print(ngaysinh[row])
print(namsinh[row])
}
}
And this is CustomCell Class:
import UIKit
class CustomCell: UITableViewCell {
@IBOutlet weak var hoten: UILabel!
@IBOutlet weak var ngaysinh: UILabel!
@IBOutlet weak var namsinh: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
Upvotes: 0
Views: 12001
Reputation: 61
You need to self tableView Delegate and Datasource either in viewDidLoad or from Stroyboard.
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
Upvotes: 0
Reputation: 1898
I had this issue too and the above didn't go all the way for me. What I needed to also do was to make sure I had the dataSource and delegate outlets connected to the view controller (and that my view controller class included UITableViewDelegate and UITableViewDataSource). Check this in case the above doesn't fix it for you.
Upvotes: 0
Reputation: 9503
Do following :
Note:
1. Make a space between as
and customCell
like this as! CustomCell
. And its better for iOS if you declare
variable cell like this let cell : CustomCell = ..........
because on this case x-code know your variable type. [may be sometimes it dos not create conflicts but it is not in a good practice of coding].
2 .Possibly change your tableview outlet name @IBOutlet var tableView: UITableView!
to any other name because tableView
is already in build name taken by the iOs if you use same variable then it overrides and caused conflict for the x-code.
edit
Instead of self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as!CustomCell
remove self from the line because your are not dequeueing your tableView
object, here you just need to dequeue your parameter of the cellForRow
method.
Use below code :
let cell : CustomCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell
Solution
Use this below demo image to bind your delegates
and datasource
from the storyboard
,I have added output after doing this, check.
Demo Image
Output from your project:
Upvotes: 3
Reputation: 1956
I think, please write this in your viewDidLoad method and try,
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
// Do any additional setup after loading the view.
}
Upvotes: 0