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;
}
Related
I have a problem in my storyboard.It is working fine but when i try to change the content property of UITableView it caused following error
Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit/UIKit-2903.23/UITableView.m
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
I want to design a grouped tableview with static cell.Thanks in advance
Code
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}
if you use static cell, just comment all the UITableViewDatasourceDelegate method. so comment the following code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}
Wish this will help you!
Instead of using:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//-> This is used when using prototype cells (e.g) Dynamic TableView
Use:
UITableViewCell *cell = [super tableView:tableView
cellForRowAtIndexPath:indexPath];
//-> Since you chose Static TableCells you don't need to reuse instead use the cells you created in nib
I have encountered same issue, and I think I found the way to work this out.
Create a Controller extends from UITableViewController to wrap its controller, it will have default UITableViewDelegate and UITableViewDataSource methods.
Remove UITableView delegate and datasource
Delete default UITableViewDelegate and UITableViewDataSource methods from your controller file, coz it's static cells, so if these exist, will not appear
Hope it helps.
When using storyboard the tableview cell must be registered in the storyboard.That means cell must be added to the table and its identifier must be set.This identifier should be used in the code in cellForRowAtIndexpath
EDIT
In case of static cells the each purticular cells needed to be created in the nib and filled content via code or nib
Read this
Loading Table View Cells from a Storyboard
Excellent starter
You are not allowed to use static cells in a normal UITableView.
Instead you need to use UITableViewController to go with the static cells.
this and this may or might help.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [YourArr count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"cell %d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = nil;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
Add this code in cellForRowAtIndexPath
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
else
{
UIView *subview;
while ((subview= [[[cell contentView]subviews]lastObject])!=nil)
[subview removeFromSuperview];
}
I have a UIViewController setup in a storyboard with a tableview and UISearchDisplayController.
I am trying to use a custom prototype cell from self.tableview (which is connected to main tableview in the storyboard). It works fine if self.tableview had returned at least 1 cell when I load my view, but if self.tableview doesn't load a cell (as there is no data), and I load up the UISearchBar and search, the cellForRowAtIndexPath: method crashes:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomSearchCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"CustomSearchCell" forIndexPath:indexPath];
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
-(void)configureCell:(CustomSearchCell *)cell atIndexPath:(NSIndexPath *)indexPath {
User *user = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.nameLabel.text = user.username;
}
Errors:
*** Assertion failure in -[UITableViewRowData rectForRow:inSection:heightCanBeGuessed:]
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for rect at invalid index path (<NSIndexPath: 0x9ef3d00> {length = 2, path = 0 - 0})
My fetchedResultsController seems to have data (1 section, 2 rows) at the point the above method is called. It crashes on the dequeueReusableCellWithIdentifier line.
Any pointers/ideas? It should dequeue the prototype cell from self.tableview, but my guess is there was none created in self.tableview so this is the cause?
UISearchDisplayController manages it's own UITableView (filtered table), in addition to having your primary table. The cell identifier in the filtered table doesn't match your primary table. You also want to fetch the cell not by indexPath as both tables can be vastly different from each other with respect to number of rows, etc.
So instead of doing this:
UITableViewCell *cell =
[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
instead do this:
UITableViewCell *cell =
[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
I solved this by duplicating the prototype cell into a new xib:
In viewDidLoad:
[self.searchDisplayController.searchResultsTableView registerNib:[UINib nibWithNibName:#"CustomSearchCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:#"CustomSearchCell"];
And updated cellForRowAtIndexPath to use the method's tableview not the original self.tableview:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomSearchCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CustomSearchCell" forIndexPath:indexPath];
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
If you use à UISearchDisplayController in the methode celForRowAtIndexPath, you must use the tableView parameter methode and not the retain pointer of the controller.
try with this code
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomSearchCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CustomSearchCell" forIndexPath:indexPath];
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.
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;
}
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];