0

I have a Custom Cell designed in storyboard, it is inside a UITableViewController which is working fine with the custom cell.

Now, I'm trying to use the same cell on a UITableViewController with a UISearchDisplayController and it is not working.

This is my method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {

    static NSString *CellIdentifier = @"CustomCell";

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier ];
    }

    cell.titleLabel.text = object[@"title"];
    cell.subtitleLabel.text = object[@"subtitle"];

    return cell;
}

It just return white, regular cells, and if I use the default cell.textLabel.text it shows my objects.

Jorge
  • 1,492
  • 1
  • 18
  • 33

3 Answers3

0

Try this, set Tag on storybord

https://i.stack.imgur.com/eVXPE.jpg

image from AppCoda

And change your method

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {

static NSString *CellIdentifier = @"CustomCell";

CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier ];
}

//Change this - START

UILabel *titleLabel = (UILabel *)[cell viewWithTag:100];
UILabel *subtitleLabel = (UILabel *)[cell viewWithTag:101];

titleLabel.text = object[@"title"];
subtitleLabel.text = object[@"subtitle"];

//Change this - END

return cell;
}
Quver
  • 1,408
  • 14
  • 21
0

If you have made the custom cell inside a tableview on the storyboard I don't know if you can use it in another tableview either remake the custom cell in the new tableView on the story board or make a seperate custom .xib just for the cell and load them in you cellForRow methods with something like..

CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
    [tableView registerNib:[UINib nibWithNibName:@"MyCustomCellNib" bundle:[NSBundle mainBundle] forCellReuseIdentifier:CellIdentifier];
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}
user2877496
  • 233
  • 1
  • 9
0

Or even better, you could just make a small change in your existing code, and hopefully it will work:

From:

CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

To:

CustomCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

Or in Swift

From:

var cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier) as! CustomCell

To:

var cell = self.tableView.dequeueReusableCell(withIdentifier: CellIdentifier) as! CustomCell

Credits

inigo333
  • 3,088
  • 1
  • 36
  • 41