Question

I have a prototype cell created in storyboard with all the tags working/displaying correctly. I wanted to create a duplicate prototype cell but change some of the visual elements, so that I can display both the original and new prototype cell in one table. I copied my old prototype cell in storyboard, giving it a new identifier, and redesigned it to look like the new cell. However, in my new cell, one of the views is being initialized as nil (subView). I really can't figure out why this is the case since this view loads up correctly in the old prototype cell, yet it just won't initialize in the new cell (even though it has the same exact viewWithTag number). From all the reading I've been doing, I believe it has to do with the whole cell reuse idea, but I can't pinpoint the problem...

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

  static NSString *AnnouncementCellIdentifier = @"AnnouncementCell";
  static NSString *EventCellIdentifier = @"EventCell";
  static NSString *CellIdentifier = @"TBD";


  if(self.segmentedControl.selectedSegmentIndex == 0){
    GenericPost *announcement = [self.announcements objectAtIndex:indexPath.section];
    if ([announcement isMemberOfClass: [Event class]]){
      CellIdentifier = EventCellIdentifier;
    }
    else {
      CellIdentifier = AnnouncementCellIdentifier;
    }
  }


  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  NSLog(@"CellIdentifier: %@",CellIdentifier);
  if (!cell) { // THIS IS NEVER CALLED JUST AS AN FYI
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  }


 UIView *contentView = (UIView *)[cell viewWithTag: 0];
  UIView *subView = (UIView *)[cell viewWithTag:0];


  if(self.segmentedControl.selectedSegmentIndex == 0){
    GenericPost *announcement = [self.announcements objectAtIndex:indexPath.section];
    contentView = (UIView *)[cell viewWithTag: 100];
    subView = (UIView *)[cell viewWithTag:101]; // THIS LINE RIGHT HERE SHOWS MY SUBVIEW IS INITIALIZED TO NIL WHEN CELLIDENTIFIER = EventCell, yet it works fine for AnnouncementCell

Printout:

2014-03-03 19:17:47.480 MyApp[23213:70b] CellIdentifier: EventCell
2014-03-03 19:17:47.481 MyApp[23213:70b] view is a UITableViewCell with tag = 0
2014-03-03 19:17:47.481 MyApp[23213:70b]     view is a UITableViewCellScrollView with tag = 0
2014-03-03 19:17:47.482 MyApp[23213:70b]         view is a UITableViewCellContentView with tag = 100
2014-03-03 19:17:47.482 MyApp[23213:70b]             view is a UIView with tag = 106
2014-03-03 19:17:47.482 MyApp[23213:70b]                 view is a UISegmentedControl with tag = 107
2014-03-03 19:17:47.482 MyApp[23213:70b]                     view is a UISegment with tag = 0
2014-03-03 19:17:47.483 MyApp[23213:70b]                         view is a UISegmentLabel with tag = 0
2014-03-03 19:17:47.483 MyApp[23213:70b]                         view is a UIImageView with tag = -1030
2014-03-03 19:17:47.483 MyApp[23213:70b]                     view is a UISegment with tag = 0
2014-03-03 19:17:47.484 MyApp[23213:70b]                         view is a UISegmentLabel with tag = 0
2014-03-03 19:17:47.484 MyApp[23213:70b]                         view is a UIImageView with tag = -1030
2014-03-03 19:17:47.484 MyApp[23213:70b]                     view is a UISegment with tag = 0
2014-03-03 19:17:47.485 MyApp[23213:70b]                         view is a UISegmentLabel with tag = 0
2014-03-03 19:17:47.485 MyApp[23213:70b]                         view is a UIImageView with tag = -1030
2014-03-03 19:17:47.485 MyApp[23213:70b]             view is a UIView with tag = 999
2014-03-03 19:17:47.486 MyApp[23213:70b]                 view is a UILabel with tag = 0
2014-03-03 19:17:47.486 MyApp[23213:70b]                 view is a UILabel with tag = 0
2014-03-03 19:17:47.486 MyApp[23213:70b]                 view is a UILabel with tag = 0
2014-03-03 19:17:47.487 MyApp[23213:70b]                 view is a UILabel with tag = 0
2014-03-03 19:17:47.487 MyApp[23213:70b]                 view is a UILabel with tag = 0
2014-03-03 19:17:47.487 MyApp[23213:70b]                 view is a UIScrollView with tag = 103
2014-03-03 19:17:47.487 MyApp[23213:70b]                     view is a UITextView with tag = 104
2014-03-03 19:17:47.488 MyApp[23213:70b]                         view is a _UITextContainerView with tag = 0
2014-03-03 19:17:47.488 MyApp[23213:70b]                             view is a UITextSelectionView with tag = 0
Was it helpful?

Solution

To see what your view hierarchy really looks like, please add the following:

- (void)logTags:(UIView *)view indent:(NSInteger)indent {

    NSLog(@"%*sview is a %@ with tag = %d", indent, "", view.class, view.tag);

    for (UIView *subview in [view subviews]) {
        [self logTags:subview indent:indent+4];
    }
}

then, after dequeueing a cell, log that cell. (pick a single row to limit the amount of output)

UITableViewCell *cell = // ....

if (indexPath.row == 0) {
    [self logTags:cell indent:0];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top