malena
malena

Reputation: 948

Why is UItableViewCell viewWithTag returning nil?

I am using IOX8 and Xcode 6. I created a table with a customtype cell in storyboard, and I placed a label in it. I entered a tag number for the label in the IB, so that I could retrieve a reference to the label in my code. But when I try to get a reference to the label inside my "cellForRowAtIndexPath", using viewWithTag I get a NIL for the label :

This are the two lines of code that are important in cellForRowAtIndexPath:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PlayerCell"];


UILabel *nameLabel = (UILabel *)[cell viewWithTag:1];

The "cell viewWithTag:1" line returns NIL, I don't understand why.

I also tried "cell.contentView withWithTag:1" and that did not work either.

Is this a bug in IOS 8 or Xcode 6?

thanks

-Malena

Upvotes: 10

Views: 6251

Answers (6)

Sharukh Mastan
Sharukh Mastan

Reputation: 1591

I had mistakingly made 2 prototypes of UITableViewCell, one on the storyboard where I gave it the tag number and one in my code where I registered the Nib file of it. This caused a mix up and I couldn't get a hold of the view with the tag. My e.g., let nib = UINib(nibName: "DetailTableViewCell", bundle: nil) tableView.register(nib, forCellReuseIdentifier: "headerViewCell")

Removing the above snippet from my code solved the problem. Hope this helps anyone with a similar problem.

Upvotes: 0

William Hu
William Hu

Reputation: 16151

Try to make the view 'installed', check this link:

Upvotes: 2

Marco Castanheira
Marco Castanheira

Reputation: 131

If the initial view controller was my tableview, then everything worked as expected. When it came from another view controller I had issues.

In my case, in previous view controller I had:

RoutePreview *rs = [[RoutePreview alloc] init];
rs.rota = [availableRoutes objectAtIndex:indexPath.row];

[self.navigationController pushViewController:rs animated:YES];

I've replaced this with

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"SBIPhone" bundle:[NSBundle mainBundle]];
RoutePreview *xpto = [storyboard  instantiateViewControllerWithIdentifier:@"RouteSummary"];
xpto.rota = [availableRoutes objectAtIndex:indexPath.row];
[self.navigationController pushViewController:xpto animated:YES];

The issue was gone.

Basically instead of instantiate the tableviewcontroller directly, instantiate it by the identifier, and no more nill cell in tableView dequeueReusableCellWithIdentifier or cell viewWithTag: returning nil.

Upvotes: 0

Karthik damodara
Karthik damodara

Reputation: 1955

Uncheck Size Classes option in the storyboard and clean & build the app. It should work now.

Upvotes: 11

angeldsWang
angeldsWang

Reputation: 123

I met the same problem when I was using IOS8 and Xcode 6.1. I found that if I enable the Size Classes, viewWithTag always return nil. I think it may be a bug for Xcode 6.x. Just disable the Size Classes, everything goes ok.

Upvotes: 5

Ivane Gegia
Ivane Gegia

Reputation: 1

Try to debug View Hierarchy.

  • Run your application in debug mode;
  • Goto view, where UITableViewController sits;
  • In Xcode click: Debug->View Debugging->Capture View Hierarchy;
  • Inspect weather tag(s) are assigned properly.

Upvotes: 0

Related Questions