Jason
Jason

Reputation: 1067

Custom UITableViewCell in swift

I have an custom tableview cell in Swift as an xib file, which is fine and i have an swift file that links to it.

like so :

import UIKit

class CustomTableViewCell: UITableViewCell {

    @IBOutlet weak var CellTitle: UILabel!
    @IBOutlet weak var CellLocation: UILabel!
    @IBOutlet weak var CellDate: UILabel!
    @IBOutlet weak var Type: UIImageView!
    @IBOutlet weak var TypeText: 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
    }

}

the IBOutlets are just links to the UILabels and UIImageViews but these don't need any values at the moment because they are added in the design at the moment.

I have an ViewController file which is linked to a view controller on the storyboard, and this has a tableview on it. and this is the code I have used for this, but how can i use the customCell I have created on this tableview (view controller).

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

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

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("tableCell", forIndexPath: indexPath) as UITableViewCell


        return cell
    }

}

Any help would be brilliant, thanks

I have added :

 var nib = UINib(nibName: "CustomTableViewCell", bundle: nil)
        tableView.registerNib(nib, forCellReuseIdentifier: "customCell")

to register the nib Name and register the cell in the tableview , and I have also changed the cellforindexpath to :

var cell:CustomTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("CustomCell") as CustomTableViewCell

Upvotes: 0

Views: 181

Answers (2)

Beslan Tularov
Beslan Tularov

Reputation: 3131

try this!!

    override func viewDidLoad() {
            super.viewDidLoad()
    self.tableView.delegate = self
    self.tableView.datasourse = self 
    self.tableView.registerNib(nib, forCellReuseIdentifier: "customCell")
       }

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("tableCell", forIndexPath: indexPath) as UITableViewCell


        return cell
    }

Upvotes: 0

bluedome
bluedome

Reputation: 2459

cellIdentifier is different. typo?

tableView.registerNib(nib, forCellReuseIdentifier: "customCell")

var cell:CustomTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("CustomCell") as CustomTableViewCell

Upvotes: 1

Related Questions