0

i am trying to display content in a custom cell in a table view. I started with a master detail application and tried to customize it. I created a new xib with the custom cell and connected it to a class and created the needed outlets.

This is the MasterView Controller (removed all functions I did not edit)

import UIKit

class MasterViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        var nipName=UINib(nibName: "SimpleTableCell", bundle:nil)
        self.tableView.registerNib(nipName, forCellReuseIdentifier: "cell")
        self.tableView.registerClass(SimpleTableCell.self, forCellReuseIdentifier: "cell")


        //Create Add Button
        let addButton = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "insertNewObject:")
        self.navigationItem.rightBarButtonItem = addButton
        loadInitialData()
        tableView.reloadData()

    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var  cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as SimpleTableCell
        cell.loadItem(user:"user1", hoop: "hoop1", post: "There is text in the table")
        return cell
    }
}

This is the Class for the custom cell

import UIKit

class SimpleTableCell : UITableViewCell{
    @IBOutlet var userLabel: UILabel
    @IBOutlet var hoopLabel: UILabel
    @IBOutlet var postLabel: UILabel


    func loadItem(#user: String, hoop: String, post: String) {
        userLabel = UILabel()
        hoopLabel = UILabel()
        postLabel = UILabel()
        userLabel.text = user
        hoopLabel.text = hoop
        postLabel.text = post 
    }

}

However I always get a "can't unwrap optional.none" in the loadItem function as soon as the text of userLabel is about to be changed - I don't get it because I initialise the labels right before I try to add text - or Am I wrong?

Thanks for any help.

dschlossfr
  • 377
  • 5
  • 12
  • 2
    no need to initialize your labels just before setting text. If you have nib then connect labels from nib to outlet – Johnykutty Jul 15 '14 at 10:51

2 Answers2

0

ok somehow installing the newest xcode beta and recreating the project solved the problem... Thanks for your help.

dschlossfr
  • 377
  • 5
  • 12
-1

In your view did load you are registering class and nib of same cell for same cell identifier. If you have an xib drag and drop 3 labels in the cell and connect them to the IBOutlets, Also remove the line self.tableView.registerClass(SimpleTableCell.self, forCellReuseIdentifier: "cell"). If you are not using xib create labels, set frame and add them to the cell as subview in the method init(style: UITableViewCellStyle, reuseIdentifier: String!), also remove the line self.tableView.registerNib(nipName, forCellReuseIdentifier: "cell")

Johnykutty
  • 12,091
  • 13
  • 59
  • 100
  • I am using xib - the IB Outlets are connected via drag & drop. When I remove self.tableView.registerClass(SimpleTableCell.self, forCellReuseIdentifier: "cell") the result is another error. SIGABRT on the call of var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as SimpleTableCell – dschlossfr Jul 15 '14 at 16:40
  • have you changed the custom class of your cell in xib? – Johnykutty Jul 16 '14 at 04:42
  • yes the cell is assigned to the custom class "SimpleTableCell" - which is shown in my first post. – dschlossfr Jul 16 '14 at 12:03