13

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.

kubilay
  • 5,047
  • 7
  • 48
  • 66

4 Answers4

54

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;
}
Community
  • 1
  • 1
nikravi
  • 870
  • 9
  • 11
  • 1
    This may have solved the problem, but for most people the problem is that they do not create a new cell if one is not already in the queue for that UITableView. The answers below more appropriate explain how to handle this. – FilmJ Aug 16 '12 at 06:25
2

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];
  }
FilmJ
  • 2,011
  • 3
  • 19
  • 27
0

I'm not sure about how this should work in storeboarding.

But normally you would check if the [tableView dequeueReusableCellWithIdentifier:CellIdentifier] returns a cell. Because if the cell in not loaded before or there aren't any cells to reuse you will have to create a new cell:

SpeakerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
   cell = [[SpeakerCell alloc] initWithIdentifier: CellIdentifier];
}

Also when in declaring local variables in Objective-C we tent not to capitalize the first letter.

rckoenes
  • 69,092
  • 8
  • 134
  • 166
  • No visible @interface for 'SpeakerCell' declares the selector 'initWithIdentifier:'? – kubilay Apr 17 '12 at 10:28
  • Have you setup the cell as a `nib`? Then you will have to load the `nib` other which you can create the methode `initWithIdentifier` and set up the cell in this method. – rckoenes Apr 17 '12 at 11:15
  • I don't use any nib, I am using a UITableViewCell class and declare cell identifier in tableview. What should initWithIdentifier method contain? return self? by the way why no capitalize first letters in Objective-C? Most examples contains so, didn't know that it was wrong. – kubilay Apr 17 '12 at 16:45
  • Well i'm sure about the way the loading of cell should work when using a storyboard. But about the capitalie of the first letter this is in the Objective-c style guide: https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingIvarsAndTypes.html#//apple_ref/doc/uid/20001284-1001783 – rckoenes Apr 18 '12 at 06:58
0

I had the same issue, with custom cells (built in Storyboard) not being drawn as soon as the first letter was put in the search field. The search was successful however.

Finally I found a good tutorial from Brenna Blackwell suggesting to configure manually the cell drawing in the corresponding subclass of UITableViewCell, adding UILabels and other items.

phbardon
  • 127
  • 9