Assertion Failure When Using Uisearchdisplaycontroller in Uitableviewcontroller

Assertion failure when using UISearchDisplayController in UITableViewController

Try using self.tableView instead of tableView in dequeueReusableCellWithIdentifier:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"BreedCell"];

//Create PetBreed Object and return corresponding breed from corresponding array
PetBreed *petBreed = nil;

if(tableView == self.searchDisplayController.searchResultsTableView)
petBreed = [_filteredBreedsArray objectAtIndex:indexPath.row];
else
petBreed = [_breedsArray objectAtIndex:indexPath.row];

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = petBreed.name;

return cell;
}

This code works pretty well

Note

If you have custom height cells, do not use

[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

Use this instead

[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

Assertion failure in -[UITableView layoutSublayersOfLayer:]

As a general solution to this problem, or at least a way of finding the cause of it:

  • Turn on exception breakpoints
  • Subclass UITableView and override layoutSublayersOfLayer:, just calling super
  • Run your app - you will stop in your new method
  • In the debugger, type po [self _autolayoutTrace]

This will show you a printout of every view in the window, with the views where auto layout has not been able to come up with a solution highlighted by asterisks or AMBIGUOUS LAYOUT. These are the views you need to investigate the constraints for.

Display issue when using UISearchBar with a table view controller and segueing to another view

There's actually nothing wrong with using either method to dequeue your cell for the main tableView, although the indexPath variant seems to be Apple's preferred option these days.

For the searchResultsTableView however, avoid specifying the indexPath as it's not necessarily valid for your view controller's tableView. That is:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyTableViewCell *cell;
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]) {
cell = [self.tableView dequeueReusableCellWithIdentifier:@"MyCell"];
} else {
cell = [self.tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];
}
// configure the cell
}

For this to work correctly, you also need to amend your other UITableViewDataSource method. Instead of:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return (searchResults) ? (searchResults.count) : (testData.count);
}

Do:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]) {
return searchResults.count;
}
return testData.count;
}

UISearchDisplayController with UITableViewController

It was an extra self.tableView.scrollEnabled = YES which was called as soon as the search data request was finished. Hope it helps anyone with the same problem in the future.

Assertion failure in UITableView configureCellForDisplay:forIndexPath:

you are never creating a cell, you just try to reuse a dequeued cell. but as you never created one, there is none.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
static NSString *cellIdentifier = @"cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}

NSString *currentValue = [shareValues objectAtIndex:[indexPath row]];
[[cell textLabel]setText:currentValue];
return cell;
}

or try (only iOS 6+)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
static NSString *cellIdentifier = @"cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

NSString *currentValue = [shareValues objectAtIndex:[indexPath row]];
[[cell textLabel]setText:currentValue];
return cell;
}

from UITableView.h

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier;  // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier
forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); // newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered

-dequeueReusableCellWithIdentifier: will always need a check, if a cell was returned, while

-dequeueReusableCellWithIdentifier:forIndexPath: can instantiate new one.

iOS - Using a UISearchDisplayController on a UITableViewController with static cells

I managed to solve the error with the help of a colleague. After paying more attention to the call stack, we realized that the app was crashing in a datasource method (index 3).

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 5 beyond bounds [0 .. 4]'
*** First throw call stack:
(
0 CoreFoundation 0x000000010188c795 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001015ef991 objc_exception_throw + 43
2 CoreFoundation 0x000000010184502f -[__NSArrayI objectAtIndex:] + 175
3 UIKit 0x00000001006d97df -[UITableViewDataSource tableView:indentationLevelForRowAtIndexPath:] + 115
4 UIKit 0x0000000100318a08 __53-[UITableView _configureCellForDisplay:forIndexPath:]_block_invoke + 1574
5 UIKit 0x00000001002a60ac +[UIView(Animation) performWithoutAnimation:] + 70
[...]

So, I added this and the results display perfectly fine now.

- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 0;
}

This actually solved two issues, as I noticed my search results had varying indentations before adding that method as well.



Related Topics



Leave a reply



Submit