UITableView Using Swift

You should register the class for the cell. For that do change this line of code to

self.tableView.registerClass(TapCell1.classForCoder(), forCellReuseIdentifier: "Cell")

Edit

You code is looks fine i checked it

//cell.labelText.text="Cell Text"

cell.textLabel.text="Cell Text"   // use like this

I have now been able to get Custom UITableViewCell to work.

Works on

  • Runs on Xcode 6 beta 6
  • Runs on Xcode 6 beta 5

  • iOS is 7.1

How

  • I have created a ".xib" file for the cell.
  • I copy it into the storyboard.
  • Via the storyboard I give it a Identifier.
  • I make sure it is a sub child of a tableview

Doing it this way, you do not need to register a class / nib etc.

This is my custom cell.

import UIKit

class TestCell: UITableViewCell {

    @IBOutlet var titleImageView: UIImageView!
    @IBOutlet var titleLabel: UILabel!

    override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

In your view, or where ever you extend "UITableViewDataSource". Make sure "cell2" is the same as the "Identifier" that you gave it via the storyboard.

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

    var cell:TestCell = tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as TestCell

    // Example of using the custom elements.
    cell.titleLabel.text = self.items[indexPath.row]

    var topImage = UIImage(named: "fv.png")
    cell.titleImageView.image = topImage

    return cell
}

uitableviewcell


The solution is most likely pinpointed to setting the cell height manually as such:

override func tableView(tableView:UITableView!, heightForRowAtIndexPath indexPath:NSIndexPath)->CGFloat
{
    return 44
}

I believe it's an Xcode beta 6 bug.


I finally did it.

For TapCell1.swift

import UIKit

class TapCell1: UITableViewCell {

    @IBOutlet var labelTitle: UILabel

    init(style: UITableViewCellStyle, reuseIdentifier: String!) {
        super.init(style: UITableViewCellStyle.Value1, reuseIdentifier: reuseIdentifier)
    }
}

For NextViewController.swift

import UIKit

class NextViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var tableView: UITableView

    var ListArray=NSMutableArray()

    override func viewDidLoad() {
        super.viewDidLoad()

        let nibName = UINib(nibName: "TapCell1", bundle:nil)
        self.tableView.registerNib(nibName, forCellReuseIdentifier: "Cell")

        for i in 0...70 {
            ListArray .addObject("Content: \(i)")
        }
    }


    func tableView(tableView: UITableView!, numberOfRowsInSection section: Int)->Int {
       return ListArray.count
    }

    func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 51
    }

    func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
        return 1
    }

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as TapCell1

        //cell.titleLabel.text = "\(ListArray.objectAtIndex(indexPath.item))"

        cell.labelTitle.text = "\(ListArray.objectAtIndex(indexPath.row))"

        return cell
    }
}

My working code link: CUSTOMISED TABLE