UITableView Content "Static cell" not working in iOS7 - iphone

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];
}

Related

iOS 6.0 Storyboard: Set static cell selected

I have created 5 static cells in UITableViewController scene. Everything working fine but I am not able to set the particular cell selected! If it is prototype cell then its simple but how could I set static cell selected? Following code raises BAD memory access exception! I have done this based on discussion on apple thread but not sure what's wrong! Could someone help please.
- (void)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
[super tableView:tableView cellForRowAtIndexPath:indexPath];
if(indexPath.section == _rating)
[tableView cellForRowAtIndexPath:indexPath].accessoryType =UITableViewCellAccessoryCheckmark;
}
Fixed this. I have to return UITableViewCell return type and have to used this method like below. It was recursive calling the methods and hence was failing.
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
if(indexPath.section == _rating)
cell.accessoryType = UITableViewCellAccessoryCheckmark;
return cell;
}

Error returning cell from tableView

I am relatively new to using table view in ios. I am trying to edit data using different view and update the values from original view. I set cell identifier and wrote following code.
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"NameIdentifier";
Item *currentItem=[self.items objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
cell.textLabel.text=currentItem.itemName;
return cell;
}
But I get the following error:
NSInternalInconsistencyException',
reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
You need to check and make sure dequeueReusableCellWithIdentifier was able to dequeue a cell. It's crashing because it doesn't return a cell every time. If you were unable to dequeue a reusable cell you need to create a new one. Your code should look like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"NameIdentifier";
Item *currentItem=[self.items objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
// Configure the cell...
cell.textLabel.text=currentItem.itemName;
return cell;
}

Unable to Populate TableView In PopOverController - Objective C

I have a UIButton called as UploadButton. I am using the following lines of code which takes care of the action which should happen on clicking it ::
-(IBAction)UploadButtonPressed:(id)sender{
self.Upload = [[UploadSpaceTableViewController alloc]
initWithStyle:UITableViewStylePlain];
self.UploadTableViewPopover = [[UIPopoverController alloc]
initWithContentViewController:Upload];
[self.UploadTableViewPopover presentPopoverFromBarButtonItem:sender
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
Here, UploadSpaceTableViewController is a separate class which I have made. It has the following functions in it ::
- (void)viewDidLoad
{
[super viewDidLoad];
self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(150.0, 140.0);
self.keys1 = [NSMutableArray array];
[keys1 addObject:#"Red"];
[keys1 addObject:#"Green"];
[keys1 addObject:#"Blue"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [keys1 count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
NSString *key1 = [keys1 objectAtIndex:indexPath.row];
cell.textLabel.text = key1;
return cell;
}
Basically, all I want is to display a UItableView inside a UIPopOverController on the click of my UploadButton.
However, on running the above lines of code I am getting the following erre in gdb ::
splitView[4486:f803] *** Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-1914.84/UITableView.m:6061
2012-06-27 14:05:05.531 splitView[4486:f803] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:
This is the first time that I am trying to display a UITableView inside a UIPopoverController. I have tried a lot of variations in the code but, I am unable to sort it out. Can someone help me out ?? Thanks and regards.
Have you allocated your cell i think by your code it will return nill
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nill){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSString *key1 = [keys1 objectAtIndex:indexPath.row];
cell.textLabel.text = key1;
return cell;
}
may this will help you...
That error means that you're passing an invalid argument when you're assigning a value to keys1, self.keys1 = .... If you're, e.g., passing a NSMutableArray to an NSDictionary property, you'll get that error.
The problem is the following line:
self.keys1 = [NSMutableArray array];
Your UploadSpaceTableViewController has no method setKeys / no property keys.
Edit for the 2nd error message:
UITableView dataSource must return a cell from
tableView:cellForRowAtIndexPath:
This means that you should implement the UITableViewDataSource method tableView:cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//configure your cell here
return myCongiuredTableViewCell;
}
Use the following code in cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if( cell == nil )
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString *key1 = [keys1 objectAtIndex:indexPath.row];
cell.textLabel.text = key1;
return cell;
}
I think this may helpful for you.

how to remove all rows of a tableView

Can anyone tell me that how to remove all rows of a tableView
Clear the datasource (for example [dataSourceMutableArray removeAllObjects]) and reload the table ([tableView reloadData])
I think you can remove all values in dataSource of tableView? Then reload the tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 0;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 0;
}
or specify return nil; instead of return cell; in cellForRowAtIndextPath function
or you can remove all values in array in cellForRowAtIndexPath like this
- (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.
// remove below line
//cell.textlabel.text=[array objectAtIndex:indextPath];
return cell;
}

Cell customization in iPhone Application

I want to make a table view in my iPhone Application.. In that table each shall contains 1 image, some text of line, some symbols, etc.. I don't now how to customize our cell of table. Please provide some help if you have..
I think you can use a default UITableViewCell to do what you described by initializing the Cell (in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath ) with the proper UITableViewCellStyle
e.g.:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
return cell;
}
which gives you:
"A style for a cell with a left-aligned label across the top and a left-aligned label below it in smaller gray text. The iPod application uses cells in this style."
Also every kind of UITableViewCell has an image-property by default.
but here are some tutorials for building custom cells:
Custom UITableViewCells with Interface Builder
http://iphone.zcentric.com/2008/08/05/custom-uitableviewcell/
You can create a cell in Interface Builder. Start with a UITableViewCell which acts like a view, and then add a UIImage, one or more labels, whatever containers to hold your symbols, etc.
Then in your cellForRowAtIndexPath, load the nib and fill in the picture and labels, etc.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
MyTableCell *cell = (MyTableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = myTableCell; // get the object from the outlet
myTableCell = nil; // make sure this can't be re-used accidentally
}
// Set up the cell...
cell.myViewController = self;
// set up image and labels here
return cell;
}