Nurdin
Nurdin

Reputation: 23883

Assertion failure in > -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:]

I got this error message when trying to addSubView() into UITableViewCell

Error message

2015-05-18 11:26:46.048 xxxxx[1128:437863] * Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit/UIKit-3347.44/UITableView.m:6245 2015-05-18 11:26:46.053 DateTick[1128:437863] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier AgeCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' *** First throw call stack: (0x1856982d8 0x196ebc0e4 0x185698198 0x18654ced4 0x18a251e84 0x100094c20 0x100095070 0x18a3d9a68 0x18a3cd890 0x18a1b9268 0x18a0d5760 0x189a1de1c 0x189a18884 0x189a18728 0x189a17ebc 0x189a17c3c 0x189a11364 0x1856502a4 0x18564d230 0x18564d610 0x1855792d4 0x18ed8f6fc 0x18a13efac 0x1000c0a9c 0x19753aa08) libc++abi.dylib: terminating with uncaught exception of type NSException

I already declared AgeCell inside UITableViewCell identifier

enter image description here

My code:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("AgeCell", forIndexPath: indexPath) as! UITableViewCell //1

        let slider =  RangeSlider(frame:cell.bounds)
        slider.minimumValue = 1;
        slider.selectedMinimumValue = 2;
        slider.maximumValue = 10;
        slider.selectedMaximumValue = 8;
        slider.minimumRange = 2;
        slider.addTarget(self, action:"updateRangeLabel", forControlEvents:UIControlEvents.ValueChanged)

        cell.addSubview(slider)
        return cell
    }

P.S: BTW my UITableViewController is a static cell, not a dynamic cell.

Upvotes: 0

Views: 7982

Answers (4)

Ipan Ardian
Ipan Ardian

Reputation: 1

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

        let item = items[indexPath.row]

        cell.nameLabel.text = item.name

        return cell
    }

Upvotes: 0

Ewan Mellor
Ewan Mellor

Reputation: 6847

You need to register your cell's nib with the UITableView. This way, the UITableView will know that your reuseIdentifier "AgeCell" matches the cell. You usually do this in viewDidLoad for your view controller.

In Objective C:

-(void)viewDidLoad {
    [super viewDidLoad];

    UINib * nib = [UINib nibWithNibName:@"AgeCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"AgeCell"];
}

In Swift:

override func viewDidLoad() {
    super.viewDidLoad()

    let nib = UINib(nibName: "AgeCell", bundle: nil)
    self.tableView.registerNib(nib, forCellReuseIdentifier:"AgeCell")
}

Upvotes: 0

Joped
Joped

Reputation: 1108

The syntax with Swift is

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

Upvotes: 3

Icaro
Icaro

Reputation: 14845

Notice that dequeueReusableCellWithIdentifier: and dequeueReusableCellWithIdentifier:forIndexPath: are different methods.

The documentation says:

Important: You must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling this method.

Check out this Q&A. for more details about this topic

To register a NIB in swift:

UINib nib = UINib.nibWithNibName("AgeCell", bundle:nil);
self.tableView.registerNib(nib, forCellReuseIdentifier:"AgeCell");

Upvotes: 1

Related Questions