简体   繁体   中英

Swift Custom UITableViewCell not displaying data from array

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
}

}

Do following :

  1. Check your typo for cell and class name
  2. Check whether delegate and datasource of UITableview is properly binded or not
  3. If your using xib for TableViewCell then register your xib in VDL

Note: 1. Make a space between as and customCell like this as! CustomCell 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:

在此处输入图片说明

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.
}

Make sure you have set cell identifier

在此处输入图片说明

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.

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
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM