0

I have a UITableViewController where there are three UITableViewCell static

I want to create an outlet for one of the labels inside on of the cells, I couldn't drag and drop, i read over the Internet, people suggest do the following

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(CellIdentefiers.oneMenuCell.rawValue)
    let label = cell!.viewWithTag(1) as! UILabel
    label.font = UIFont(name: Constants.centuryGothic.rawValue, size: CGFloat(17))
    return cell!
}

i gave that label tag = 1

my problem is that the cell is always nil, though it has the correct identifer. look please enter image description here

What missing did I make?

update

people ask for this

enum CellIdentefiers : String {
    case numberOfPeopleCell
    case dayCell
    case timeCell
    case preferencesCell
    case oneHistoryCell
    case oneConfirmationCell
    case oneMenuCell
}
sarah
  • 1,201
  • 1
  • 9
  • 29

3 Answers3

0

Your enum needs raw values i.e.

enum CellIdentefiers : String {
    case numberOfPeopleCell = "numberOfPeopleCell"
    case dayCell = "dayCell"
    case timeCell = "timeCell"
    case preferencesCell = "preferencesCell"
    case oneHistoryCell = "oneHistoryCell"
    case oneConfirmationCell = "oneConfirmationCell"
    case oneMenuCell = "oneMenuCell"
}
dudeman
  • 1,106
  • 8
  • 19
0

Reuse identifiers are for dynamic cells. To create an outlet for a label in a static cell, you can drag an connect an IBOutlet from storyboard to the ViewController. I just tested it and it absolutely works.

EDIT: Here is an image to prove that you can connect IBOutlets to static cell labels.

enter image description here

Tim
  • 2,089
  • 1
  • 12
  • 21
  • you could drag outlet from a static cell??? what xcode is your plz ? because mine not allow me – sarah Dec 16 '15 at 01:37
  • Version 7.2, but I'm pretty sure I've been doing that for awhile. – Tim Dec 16 '15 at 01:45
  • Finally figured out how to take a screenshot while holding the connecting line. I have no idea why your Xcode wouldn't let you do this. – Tim Dec 17 '15 at 01:05
0

U need to specifically register a cell in the viewDidLoad for a given identifier:

self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cellIdentifier")

In your case you would do something like:

self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: CellIdentefiers.oneMenuCell.rawValue)

When you access it in your cellForRowAtIndexPath u need to access that identifier "cellIdentifier" in your dequeueReusableCellWithIdentifier:

 let cell = tableView.dequeueReusableCellWithIdentifier("cellIdentifier")

In your case you are exactly doing that:

let cell = tableView.dequeueReusableCellWithIdentifier(CellIdentefiers.oneMenuCell.rawValue)

The problem is these steps are generally done when you have a "custom" cell you are working with. In case you don't want to use custom cells you can simply do the following in your cellForRowAtIndexPath:

let cellIdentifier = "Cell"
var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier)
if(cel == nil){
    cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellIdentifier)
}

Lastly, @MikeAtNobel is correct. You will need to provide String values from the enum rather than int values. Hence the change suggested by him does seem valid and correct.

Karthik
  • 99
  • 2
  • 14
  • thanks for the ansewr but after ading your `registerClass` nothing changed, and i keep with the same error. secondly, i was already using the dequeu with identifer. third, No there is no dequeue here, this is a static cell and as I understood there is no deque here – sarah Dec 16 '15 at 10:21