I have a program in which there is a tableViewController which has 1 section and 3 rows. Whenever I run the app, it always crashes at dequeueReusableCellWithIdentifier:
method. I inserted a breakpoint and NSLog()
to zero in the this method. I also set the Cell Identifier
of the prototype cell in storyboard to Cell
. But the program still crashes. Here's an method that is causing the problem:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"indexPath.section = %d, indexPath.row = %d", indexPath.section, indexPath.row);
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
NSLog(@"in nil");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
return cell;
}
The crash log reads:
MyAppTake4[7463:707] indexPath.section = 0, indexPath.row = 0
2012-11-05 23:21:20.747 MyCardAppTake4[7463:707] -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:]: unrecognized selector sent to instance 0xfbc9000
2012-11-05 23:21:20.753 MyCardAppTake4[7463:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableView dequeueReusableCellWithIdentifier:forIndexPath:]: unrecognized selector sent to instance 0xfbc9000'
*** First throw call stack:
(0x3254788f 0x3459d259 0x3254aa9b 0x32549915 0x324a4650 0x7e2f7 0x31fceefb 0x31fcdfd9 0x31fcd763 0x31f71f15 0x324a61fb 0x3420aaa5 0x3420a6bd 0x3420e843 0x3420e57f 0x342064b9 0x3251bb1b 0x32519d57 0x3251a0b1 0x3249d4a5 0x3249d36d 0x315f4439 0x31f9ccd5 0x7d9f9 0x7d994)
terminate called throwing an exception(lldb)
I am interested in identifying the exact cause of error so that I do not make this mistake again.
Thanks for your help.
Thats because [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; was added in iOS 6 but not recognized by iOS 5. For iOS 5 compatibility use below method instead:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Did you register the class of the cell or the nib first?
From the Apple docs:
Important: You must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling this method.
(btw that method is only available in iOS 6)
If you're using iOS 6, then you can use the new simpler paradigm:
In viewDidLoad, register the class, in your case, that's just the UITableViewCell class:
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
Then, you can just do it like this in cellForRowAtIndexPath:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.textLabel.text = self.myArray[indexPath.row];
return result;
}
Since forIndexPath method is for ios6, I recommend you to use this snipet. Find below.
UITableViewCell *cell = [table_view dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
// **initialize** your stuff here **with their respected tag**
}
else
{
// **get** your stuff back using their tags
}
// **assign** your valiues here
return cell;
Enjoy Programming
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With