UITableViewCell - 显示viewWithTag = nil的多个单元格标识符

时间:2014-03-03 20:32:03

标签: ios objective-c uitableview

我在故事板中创建了一个原型单元格,所有标签都正常工作/显示。我想创建一个重复的原型单元格但改变一些可视元素,这样我就可以在一个表格中显示原始和新的原型单元格。我在故事板中复制了我的旧原型单元,给它一个新的标识符,并重新设计它看起来像新的单元格。但是,在我的新单元格中,其中一个视图被初始化为nil(subView)。我真的无法弄清楚为什么会出现这种情况,因为这个视图在旧的原型单元格中正确加载,但它不会在新单元格中初始化(即使它具有完全相同的viewWithTag数字)。从我一直在做的所有阅读中,我相信它与整个细胞重用的想法有关,但我无法确定问题......

- (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

打印输出:

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

1 个答案:

答案 0 :(得分:0)

要查看您的视图层次结构的真实情况,请添加以下内容:

- (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];
    }
}

然后,在将单元格出列后,记录该单元格。 (选择一行来限制输出量)

UITableViewCell *cell = // ....

if (indexPath.row == 0) {
    [self logTags:cell indent:0];
}