UITableView Custom Cellular SIGABRT Error

I am trying to create a custom cell for my UITableView. I use Xcode 4.2 and use the storyboard along with ARC.

I created a class to represent a custom cell as follows:

ResultsCustomCell.h

#import <UIKit/UIKit.h> @interface ResultsCustomCell : UITableViewCell { IBOutlet UILabel *customLabel; } @property (nonatomic, retain) IBOutlet UILabel* customLabel; @end 

ResultsCustomCell.m

 #import "ResultsCustomCell.h" @implementation ResultsCustomCell @synthesize customLabel; @end 

Then I applied the UITableView method in my view controller as follows: ViewController.m

 #import "ViewController.h" #import "ResultsCustomCell.h" @implementation ViewController - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } #pragma mark - View lifecycle - (void)viewDidLoad { [super viewDidLoad]; } // Set the number of items in the tableview to match the array count -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 5; } // Populate TableView cells with contents of array. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CellIdentifier"; ResultsCustomCell *myCell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"]; myCell.customLabel.text = @"helloWorld"; return myCell; } // Define height for cell. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 80; } @end 

The application is successful, but then it will instantly work and give me the following error:

 Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:' 

What part am I missing?

+5
source share
7 answers

I'm not quite sure what I did to solve my problem, but now it works correctly.

Here is what I now have in my ViewController.m

Note. The results of CustomCell.h and .m are the same as above.

 #import "ViewController.h" #import "ResultsCustomCell.h" @implementation ViewController - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } #pragma mark - View lifecycle - (void)viewDidLoad { [super viewDidLoad]; } // Set the number of items in the tableview to match the array count -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 5; } // Populate TableView cells with contents of array. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CellIdentifier"; // Make sure there are no quotations (") around the cell Identifier. ResultsCustomCell *myCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; myCell.customLabel.text = @"helloWorld"; return myCell; } // Define height for cell. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 80; } @end 

Then I made sure:

  • The table view cell has the correct user class (ResultsCustomCell).
  • The cell identifier for the UITableViewCell was right in Interface Builder.
  • UILabel in the interface builder was correctly associated with IBOutlet.
  • The UITableView had the correct controller (ViewController)

After using this code and checking everything above, it works.

0
source

This is an annoying problem because the debugger does not let you know what happened. It will be broken into an exception, and then just shut down if you click Continue.

This made me roll for 30 minutes, until I right-clicked on the UILabel in the user cell that was causing me problems, which revealed the answer: one of the controls in your cell (or, as you apparently fixed perhaps not noticing a false exit. By "false exit" I mean one that is connected to an IBOutlet property that no longer exists in your class. In my case, I changed the name of the property and rewrote it, but forgot to delete the old connection for links.

As soon as I removed the violating output, the problem disappeared.

+20
source

You forgot to create a new UITableViewCell when it was not deleted

 // Populate TableView cells with contents of array. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CellIdentifier"; ResultsCustomCell *myCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (myCell == nil) { myCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } myCell.customLabel.text = @"helloWorld"; return myCell; } 
+2
source

You forgot to add the reuse identifier ("CellIdentifier") to the storyboard against your prototype cell, so when it tries to create a new cell, the prototype with that identifier does not exist in the storyboard, and it returns nil .

+2
source
 @property (nonatomic, retain) IBOutlet UILabel* customLabel; 

Automatic reference counting (ARC) prohibits explicit invocation of retain . Try to remove this.

As for the error, you are returning a UITableViewCell , not your custom cell. In addition, you never highlight your ResultsCustomCell .

 - (ResultsCustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"CellIdentifier"; ResultsCustomCell *cell = (ResultsCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[ResultsCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } // Configure the cell. cell.textLabel.text = @"Text"; return cell; } 

Also, your subclass of UITableViewCell does not declare the (obviously required) init method.

ResultsCustomCell.h:

 #import <UIKit/UIKit.h> @interface ResultsCustomCell : UITableViewCell @property (strong, nonatomic) UILabel *myLabel; @end 

ResultsCustomCell.m:

 #import "ResultsCustomCell.h" @implementation ResultsCustomCell @synthesize myLabel; - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code } return self; } @end 

EDIT: I saw that you are using a storyboard. My suggestion may or may not help you. I have never used a storyboard.

+1
source

I also struggled with this error with the same setting as yours. I realized that for me this is due to the new iOS6 Auto Market feature. To disable it, I opened the user xib cell, and then on the right side of the utility opened the File Inspector tab (tab on the left). There I can uncheck the "Use self-timer" checkbox.

0
source

I threw this exception by mistakenly registering an identifier with the wrong class type.

An example of an invalid registration code placed in the viewDidLoad method of a ViewController registering an identifier for a class type:

 //The Identifier is register with the wrong class type self.listView.register(CustomCellClass1.self, forCellReuseIdentifier: "CustomCellClass2") 

And then we remove it from the queue and bring it to another type of class:

 let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCellClass2", for: indexPath) as! CustomCellClass2 
0
source

Source: https://habr.com/ru/post/1397462/


All Articles