Andres
Andres

Reputation: 11747

Get Class from UITableViewCell identifier

I have a big bunch of subclasses of UITableViewCell, all of these cells have a class method that returns the estimated height for the type of cell...

So what I want to do is be able to call that method just by knowing the cell identifier (I don't want to use a Switch mapping identifier with Class). I can't think of a way of doing it...

I know there is a NSClassFromString but I can't use that because my objects don't inherit from NSObject...

Any idea?

Upvotes: 1

Views: 1349

Answers (2)

pnavk
pnavk

Reputation: 4632

You could assign the class name as the cell identifier and then do this:

Class cellClass = NSClassFromString(cellIdentifier);

Upvotes: 3

Gordon Vanderbilt
Gordon Vanderbilt

Reputation: 369

You can get the cell in cellForRowAtIndexPath by calling dequeueReusableCellWithIdentifier and cast it to your custom cell type. Then you can call any method you want.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if indexPath.row == 0 {
        let cell = tableView.dequeueReusableCellWithReuseIdentifier(myIdentifier, forIndexPath: indexPath) as! MyTableViewCell
        // call class methods here
        return cell
    }
}

Upvotes: 0

Related Questions