UISearchDisplayController and UITableView prototype cell crash - iphone

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

Related

UITableView Content "Static cell" not working in iOS7

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

Change a label in a prototype cell upon button press

I have set up a tableview using custom cells as such:-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{SWHNearYouCell *cell = (SWHNearYouCell *)[tableView
dequeueReusableCellWithIdentifier:#"NearYouCell"];
SWHNearYou *aPointer = [self.thingsNearYou objectAtIndex:indexPath.row];
cell.customNearYouLabel.text = aPointer.restrauntsNearYou;
return cell;
}
I want to change the text of customNearYouLabel upon a button press but work out how to get a pointer to the cell in my -IBAction method.
Thanks
You can just handle that in your tableView:didSelectRowAtIndexPath: method by grabbing the cell.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.customNearYouLabel.text = #"New Text";
}
I presume the button is not on a table cell?
If so, you just need to update the relevant value in the self.thingsNearYou array.
If you then call [tableView reloadData] then the table will reload it's data and the text of customNearYouLabel will change.
Worked it out - needed to add in self before tableview
-(IBAction)cancel:(id)sender{
SWHNearYouCell *cell = (SWHNearYouCell *)[self.tableView
dequeueReusableCellWithIdentifier:#"NearYouCell"];
cell.customNearYouLabel.text = #"New Text";
NSLog(#"This is it: %#",cell.customNearYouLabel.text);
}
I'll need to spend some more time on it as [self.tableView reloadData]; will not update the table but I reckon that should be easier to solve.

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

how to set accessoryType when cell has been selected

I have been trying to figure out how to set the accessoryType to UITableViewCellAccessoryCheckmark when the cell is selected but am having trouble finding a decent example of this.
If you know how to do this or a good tutorial could you please let me know that would be great.
To restrict the user to just one selection, meaning to create an exclusive list of one choice only, you could follow these steps;
Firstly, have a global index path declared in your .h file to keep track of the already selected cell ->
NSIndexPath *oldIndexPath;
When you create the cells, be sure to set the accessory type to none, so that no cell is selected by default when the table is seen;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"CallIdentifier"];
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
Finally, in the didSelectRowAtIndexPath delegate method, add the following code which will remove the checkmark from the already selected cell, and add a checkmark to the newly selected one.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (oldIndexPath==nil) { // No selection made yet
oldIndexPath=indexPath;
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
else {
UITableViewCell *formerSelectedcell = [tableView cellForRowAtIndexPath:oldIndexPath]; // finding the already selected cell
[formerSelectedcell setAccessoryType:UITableViewCellAccessoryNone];
[cell setAccessoryType:UITableViewCellAccessoryCheckmark]; // 'select' the new cell
oldIndexPath=indexPath;
}
}
Hope this works out! :)
Something like this may work:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self tableView:myTableView cellForRowAtIndexPath:indexPath];
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
To answer the comment below, just push a viewController in the same method like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self tableView:myTableView cellForRowAtIndexPath:indexPath];
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
// Then push a new view
iPhoneCustomViewController *myVC = [[iPhoneCustomViewController alloc] initWithNibName:#"iPhoneCustomViewController" bundle:nil];
[self.navigationController pushViewController:myVC animated:YES];
[myVC release];
// And deselect the row (if desired)
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Did you know that:
1.) UITableView keeps track of the index paths for the rows that have been selected? It's in an array called indexPathsForSelectedRows
2.) UITableView has a flag you can set to make it either single or multiple selection. You can change it by calling the setter setAllowsMultipleSelection:(BOOL).
So, assuming that the table has been set to single selection, we can do the following in the tableView:CellForRowAtIndexPath method ...
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[[cell textLabel] setText:#"Some Text"];
NSArray *selectedIndexPaths = [tableView indexPathsForSelectedRows];
if ([selectedIndexPaths containsObject:indexPath]) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}else{
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
return cell;}
This implementation of CellForRowAtIndexPath will give you a clean checkmark with no gray background when a cell is selected. You will need to set the checkmark in the didSelectRowAtIndexPath delegate method to make sure a cell gets the checkmark the moment it gets selected.
No need to create separate ivars or anything else to keep track of what was or wasn't selected. It's all neatly contained in the UITableView as Apple intended.
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType=UITableViewCellAccessoryCheckmark;
Implement this in didSelectRowAtIndexPath delegate method
From the docs:
The delegate handles selections in this method. One of the things it
can do is exclusively assign the check-mark image
(UITableViewCellAccessoryCheckmark) to one row in a section
(radio-list style). This method isn’t called when the editing property
of the table is set to YES (that is, the table view is in editing
mode). See "Managing Selections" in Table View Programming Guide for
iOS for further information (and code examples) related to this
method.
Here is an example:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]
cell.UITableViewAccessoryType = UITableViewCellAccessoryCheckmark;
}