1

I need to load a custom cell in a UITableView. I created a custom subclass of UITableViewCell named "CustomTableViewCell". I have added a UITabelViewCell to the tableview (using drag and drop) as shown in figure. Then in file inspector I set the class of that UITabelViewCell to be "CustomTableViewCell". Here is my code:

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    @IBOutlet var tableView : UITableView

    var items = String[]()


    override func viewDidLoad() {
        super.viewDidLoad()
        items = ["Hi","Hello","How"]
        self.tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "CusTomCell")
        // Do any additional setup after loading the view, typically from a nib.
    }

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

   func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{
    var cell:CustomTableViewCell? = self.tableView.dequeueReusableCellWithIdentifier("CusTomCell") as? CustomTableViewCell
    if !cell
    {
        cell = CustomTableViewCell(style: UITableViewCellStyle.Subtitle,
            reuseIdentifier: "CusTomCell")
    }
    println("cell \(cell)")
   // cell.?.labelTitle.text = items[indexPath.row]
    cell!.labelTitle.text = "some text"
    return cell
}





    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

enter image description here

When I run my code, I get the following error: "fatal error: Can't unwrap Optional.None" as seen in the image.

Mani murugan
  • 1,792
  • 2
  • 17
  • 32

3 Answers3

4

Hi Finally i found solution for my question..please check my code..it works for me..

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    @IBOutlet var tableView : UITableView

    var items = String[]()

    override func viewDidLoad() {
        super.viewDidLoad()
        items = ["Hi","Hello","How"]
        // Do any additional setup after loading the view, typically from a nib.
    }

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

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{
        var cell:CustomTableViewCell? = self.tableView.dequeueReusableCellWithIdentifier("CusTomCell", forIndexPath: indexPath) as? CustomTableViewCell
        cell!.labelTitle.text = "some text"
        return cell
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
Atulkumar V. Jain
  • 5,102
  • 9
  • 44
  • 61
Mani murugan
  • 1,792
  • 2
  • 17
  • 32
  • 5
    The key I found for me was NOT using "registerClass". Once I did that it created the cell using the NSCoder as opposed to CGRect. – leogdion Jun 11 '14 at 11:06
  • 2
    What did you change, dude? – adnako Jun 18 '14 at 15:00
  • I removed the registration and I got the following error: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier TeamCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' – diegomen Sep 08 '14 at 16:40
2

Step 1 : Register nib in viewDidLoad() like below

var xib : UINib = UINib (nibName: "WeatherTableViewCell", bundle: nil)
self.tableView.registerNib(xib, forCellReuseIdentifier: "Cell")

Step 2 : Add the following in cellForRowIndexPath

var cell:WeatherTableViewCell? = self.tableView.dequeueReusableCellWithIdentifier("Cell") as? WeatherTableViewCell
cell!.weatherName.text = "weather" 
return cell
DineshKumar
  • 1,641
  • 15
  • 13
0

self.tableView.dequeueReusableCellWithIdentifier will only return a cell if there is one created earlier. You have to do a nil check and create a new cell if it's nil. In you case it will go wrong on the cell.labelTitle.text = because cell could be nil there.

Edwin Vermeer
  • 13,017
  • 2
  • 34
  • 58
  • Thanks for your reply..i have tried your answer..but still same issue..please see my updated question...i have updated my question in cellForRowAtIndexPath.. – Mani murugan Jun 09 '14 at 04:26
  • if i set any text to "detailTextLabel" of CustomTableViewCell,then its working..if i try to set my own custom label(labelTitle) ,then it says error.. – Mani murugan Jun 09 '14 at 04:32
  • that labelTitle declared in CustomTableViewCell class "@IBOutlet var labelTitle : UILabel" and connected using IBOutlet – Mani murugan Jun 09 '14 at 04:34