Cell autoreleased and "unrecognized selector sent to instance" - iphone

Probably my code crashes in the following method, since the getData() is called only once - at the end of this method.
Moreover the didSelectRowAtIndexPath code is the following:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath
{
[tableView deselectRowAtIndexPath:newIndexPath animated:YES];
// Set
[[GlobalObject obj] setData:[(CustomCell*)[tableView cellForRowAtIndexPath:newIndexPath] getData]];
}
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CustomCell getData]: unrecognized selector sent to instance.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 0)
{
...
return cell;
}
else
{
// Create a button table view cell
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:#"CustomCell"];
if (cell == nil)
{
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"CustomCell"] autorelease];
}
[cell setButton:[ ...];
return cell;
}
}
As shown above the cell is autoreleased and probably this is the reason the app crashed. Cell went to the autorelease pool and then getData sent to an unrecognized selector. How can I resolve the issue?

CustomCell does noth have a method called getData.
Avoid inlining, write the statements one after another. This helps in debugging. You easily can then set a breakpoint.

Related

Program crashes at dequeueReusableCellWithIdentifier:

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
I had this exception, and realised that it was caused by my stupid mistake. I had created a blank .xib file (to have a custom "Loading" cell in my UITableView), but had then drag'n'dropped a View into it. Doh!
The result was the exception message which you're seeing, plus this in the Output window:
2014-04-25 20:45:12.953 Languages[36256:60b] *** Terminating app due to uncaught
exception 'NSInternalInconsistencyException', reason: 'invalid nib registered
for identifier (LoadingCell) - nib must contain exactly one top level object
which must be a UITableViewCell instance'
The solution, of course, was to add a Table View Cell to my .xib file, copy the other UI components into it, and just leave that Table View Cell as the "root" element in the file.
Another cause for this error can be-
invalid nib registered for identifier (Cell) - nib must contain exactly one top level object which must be a UICollectionReusableView instance
I had a UIView in my xib instead of a collectionViewCell. Hope this helps.

Custom UITableViewCell using Storyboard

I'm trying to make a custom cell using storyboard.
I have tested my program with Basic cells and it worked.
Now I'have created a new class that I named NewsCell, which is containing the different Labels in the custom cell.
I have also made the cell a subclass of NewsCell. The cell identifier is "NewsCell".
This is the cellForRowAtIndexPath: method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NewsCell *cell = (NewsCell *)[tableView dequeueReusableCellWithIdentifier:#"NewsCell"]; //signal : SIGABRT
News *info = [self.news objectAtIndex:indexPath.row];
cell.titreLabel.text = info.titre;
cell.descriptionLabel.text = info.description;
return cell;
}
When I run my app, it crashes with a signal signal SIGABRT at the first ligne.
I'm sure that I've made the right connections between Labels and the table view cell.
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'The NIB data is invalid.'
I had the same problem. It turned out that I had checked "Use Autolayout" in the Interface Builder Document pane of the Storyboard file. This throws the "invalid NIB" error when running on iOS 5 because autolayout is only supported on iOS 6.
You have not created instance for the cell. Try to create instance for the cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NewsCell *cell = (NewsCell *)[tableView dequeueReusableCellWithIdentifier:#"NewsCell"]; //signal : SIGABRT
if (cell == nil) {
cell = [[[NewsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
News *info = [self.news objectAtIndex:indexPath.row];
cell.titreLabel.text = info.titre;
cell.descriptionLabel.text = info.description;
return cell;
}

App Crashes due to UITableViewScrollPositionMiddle when testing on phone but not emulator

I have a uitableview with an array of dictionary values producing a fairly big list.
That the user can select from.. once the user selects a cell I pop the view from the navigational stack and pass all the values back to the main view.. I then allow the user to go back to this view if they have made a mistake and would like to change their input.
when they click back to that view I have a method that automatically scrolls to the old selected view using UITableViewScrollPositionMiddle.
However I think its causing some errors when being used on the phone, as the emulator works fine.
here is where I think the error is happening
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
//Center previously selected cell to center of the screen
[self.tableView scrollToRowAtIndexPath:oldCheckedData atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; //<--- this method
}
and this is the error I get in the log when building it to my phone. The process in order to get this error is for the user to click on the cell in the parent view that loads the subview, the user selects a cell in the subview and is taken back to the parent view.. then when the user goes back to the same subview this shows up in the log.
2011-10-19 15:00:05.790 icode[1564:707] -[__NSArrayM section]: unrecognized selector sent to instance 0x181cd0
2011-10-19 15:00:05.797 icode[1564:707] CoreAnimation: ignoring exception: -[__NSArrayM section]: unrecognized selector sent to instance 0x181cd0
(there is no scroll effect)
then the user selects a different cell and this app crashes and this shows up in the log
2011-10-19 15:01:08.272 icode[1564:707] -[__NSArrayM row]: unrecognized selector sent to instance 0x181cd0
2011-10-19 15:01:08.275 icode[1564:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM row]: unrecognized selector sent to instance 0x181cd0'
*** First throw call stack:
(0x35e9e8b3 0x366e61e5 0x35ea1acb 0x35ea0939 0x35dfb680 0x334a76cf 0x3353c713 0x30e1d 0x3352cd69 0x335a60ab 0x35cc32ab 0x35e72a57 0x35e726bd 0x35e71293 0x35df44e5 0x35df43ad 0x30fa4fed 0x334a7dc7 0x24f7 0x24a0)
terminate called throwing an exception(gdb)
any help would be greatly appreciated.
Tableview data sources
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [arraysByLetter count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *currentLetter = [sectionLetters objectAtIndex:section];
return [[arraysByLetter objectForKey:currentLetter] count];
}
/*
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
//override state-based properties set earlier by the table view, such as selection and background colorset up shit here
}
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
//Customize cell here
cell.selectionStyle = UITableViewCellSelectionStyleNone; // no blue selection
//Replaces previously selected cells accessory view (tick)
if (indexPath == oldCheckedData)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
//Display cells with data
NSArray *keys = [self.arraysByLetter objectForKey:[self.sectionLetters objectAtIndex:indexPath.section]];
NSString *key = [keys objectAtIndex:indexPath.row];
cell.textLabel.text = key;
return cell;
}
//Section headers
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [sectionLetters objectAtIndex:section];
}
//Section header titles
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return sectionLetters;
}
It looks like the index path you have a reference to at oldCheckedData may have been deallocated when the view controller was popped from the stack. Make sure you retain it before the view controller goes away.

custom uitableViewCell in controller which inherit from UITableViewController

I want to create custom UiTableViewCell, and it works, when I create TableView, like in this example, by inherit from UIViewController. But when I create controller which inherit from UITableViewController (not UIViewController), and in xib create UITableViewCell, and of course hook up IBOutlet, i get this error:
*** Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-1448.89/UITableView.m:5678
2011-05-06 07:21:34.107 tabela[11424:207] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { return cellOne; }
This is the sample reference
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// In your case it has to be the custom cell object here.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"AnIdentifierString"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:#"AnIdentifierString"] autorelease];
}
cell.textLabel.text = #"This text will appear in the cell";
return cell;
}

UITableView error

ViewController.m
#pragma mark -
#pragma mark Table view data source
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 0;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
return cell;
}
ViewController.h
#interface PSLEViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
IBOutlet UITableView *highScoreTable;
}
#property(retain,nonatomic) UITableView *highScoreTable;
Error:
2010-12-04 02:20:15.541 PSLE[14369:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[PSLEViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x3b13080'
2010-12-04 02:20:15.542 PSLE[14369:207] Stack: (
Theres nothing wrong with that code. There may be some other underlying issues, maybe in a XIB. Are you sure the highScoreTable is connected?
Can you check the dataSource and delegate of UITableView in -(void)viewDidLoad?
They should not be nil after loaded from nib.
hai FYI are you allocate the tableView properly? check it out please
example highScoreTable = [[UITableView alloc]init];