Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom CellIdentifier is Null When Using Search Display Controller

In my tableview have custom cells that I initialize from a UITableViewCell class. I have sections for first letters of records and have an indexPath that is being created dynamically.

I wanted to add a search display controller to my tableview. So I did, created all methods to filter data. I am sure that my functions are working well because I am printing array count to screen for search results.

My problem is that the first time view loads, the data is on the screen. But when I hit the search input and type a letter, than I get 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:' error. After I used a breakpoint I saw that my custom cell is nil after searching. Data is exist, but cell is not being initialized.

Here is the code I use for custom cell initializing:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ObjectCell";

    SpeakerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    NSDictionary *myObject = [[sections valueForKey:[[[sections allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

    cell.label1.text = [myObject objectForKey:@"myValue"];
    return cell;
}

I believe I made a mistake when putting controls in IB. So I added screenshots of objects:

enter image description here

Connections inspector for my table view

Connections inspector for my table view

Connections inspector for my search display controller

enter image description here

EDIT: Problem is actually solved, I have used a UISearchBar instead of Search Display Controller but I guess this issue remains unsolved. So I'm willing to try any ways to make it work.

like image 807
kubilay Avatar asked Apr 17 '12 10:04

kubilay


2 Answers

As of here search display controller question, you need to access the self.tableView instead of tableView:

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

    // do your thing

    return cell;
}
like image 118
nikravi Avatar answered Oct 30 '22 23:10

nikravi


For those using iOS 5 and StoryBoards, you would want to use the following method instead of initWithIdentifier:

initWithStyle:(UITableViewCellStyle)stylereuseIdentifier:(NSString *)reuseIdentifier

Example:

  NSString *cellIdentifier = @"ListItemCell";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
  }
like image 29
FilmJ Avatar answered Oct 30 '22 23:10

FilmJ