2015-12-14 110 views
1

我目前正在与许多UITableViews一个应用程序,我通过在iPhone 6。最近,我开始在其他设备上测试测试所创建的应用程序,我已经注意到在iPhone 5中有任何的UITableView多个部分,它将跳过第0部分,根本不显示它。虽然在我的iPhone 6和iPhone 5s上它会显示完美。下面是一些用于显示我的Profile部分的UITableView的代码。任何帮助将不胜感激。值得一提的是,所有的设备都在运行9.1。谢谢。iPhone 5的UITableView不显示

#pragma mark - UITableViewDataSource 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 2; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (section == 0) { 
     return 2; 
    } else if (section == 1) { 
     if ([_selectedSection isEqualToString:@"captures"]) { 
      if ([_capturesArray count] > 0) { 
       return [_capturesArray count]; 
      } else { 
       return 1; 
      } 
     } else if ([_selectedSection isEqualToString:@"comments"]) { 
      if ([_commentsArray count] > 0) { 
       return [_commentsArray count]; 
      } else { 
       return 1; 
      } 
     } else if ([_selectedSection isEqualToString:@"followers"]) { 
      if ([_followersArray count] > 0) { 
       return [_followersArray count]; 
      } else { 
       return 1; 
      } 
     } else if ([_selectedSection isEqualToString:@"following"]) { 
      if ([_followingArray count] > 0) { 
       return [_followingArray count]; 
      } else { 
       return 1; 
      } 
     } 
    } 
    return 0; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (indexPath == [NSIndexPath indexPathForRow:0 inSection:0]) { 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"userCell" forIndexPath:indexPath]; 

     return cell; 
    } else if (indexPath == [NSIndexPath indexPathForRow:1 inSection:0]) { 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"menuCell" forIndexPath:indexPath]; 

     return cell; 
    } else if (indexPath.section == 1){ 
     if ([_selectedSection isEqualToString:@"captures"] && [_capturesArray count] > 0) { 
      // Get Capture 
      Capture *capture = _capturesArray[indexPath.row]; 

      // Create Cell 
      NSString *cellID = capture.cellID; 

      CaptureTableViewCells *cell = (CaptureTableViewCells *)[tableView dequeueReusableCellWithIdentifier:cellID]; 

      if (cell == nil) { 
       NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CaptureCells" owner:nil options:nil]; 
       if ([capture.cellID isEqualToString:FeaturedCellID]) { 
        cell = (CaptureTableViewCells *)[nib objectAtIndex:0]; 
       } else { 
        cell = (CaptureTableViewCells *)[nib objectAtIndex:1]; 
       } 
      } 

      return cell; 
     } else if ([_selectedSection isEqualToString:@"comments"] && [_commentsArray count] > 0) { 

     } else if ([_selectedSection isEqualToString:@"followers"] && [_followersArray count] > 0) { 

     } else if ([_selectedSection isEqualToString:@"following"] && [_followingArray count] > 0) { 

     } else { 
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"emptyCell" forIndexPath:indexPath]; 

      // Configure the cell... 

      return cell; 
     } 

    } 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"userCell" forIndexPath:indexPath]; 

    // Configure the cell... 

    return cell; 
} 

#pragma mark - UITableViewDelegate 
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{ 
    if (indexPath == [NSIndexPath indexPathForRow:0 inSection:0]) { 
     UIImageView *profileImageView = (UIImageView *)[cell viewWithTag:10]; 
     profileImageView.layer.borderColor = [self uicolorFromHex:0xffd700].CGColor; 
     profileImageView.layer.borderWidth = 5; 
     profileImageView.layer.cornerRadius = profileImageView.frame.size.width/2; 
     profileImageView.layer.masksToBounds = YES; 
    } else if (indexPath == [NSIndexPath indexPathForRow:1 inSection:0]) { 
     // Get Buttons 
     _capturesMenuButton = (UIButton *)[cell viewWithTag:1]; 
     _commentsMenuButton = (UIButton *)[cell viewWithTag:2]; 
     _followersMenuButton = (UIButton *)[cell viewWithTag:3]; 
     _followingMenuButton = (UIButton *)[cell viewWithTag:4]; 

     // Set Text Of Buttons 
     if ([_capturesArray count] > 0) { 
      [_capturesMenuButton setTitle:[NSString stringWithFormat:@"%lu", (unsigned long)[_capturesArray count]] forState:UIControlStateNormal]; 
     } 

     // Set Buttons Selector 
     [_capturesMenuButton addTarget:self action:@selector(capturesMenuButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside]; 
     [_commentsMenuButton addTarget:self action:@selector(commentsMenuButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside]; 
     [_followersMenuButton addTarget:self action:@selector(followersMenuButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside]; 
     [_followingMenuButton addTarget:self action:@selector(followingMenuButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside]; 
    } else if (indexPath.section == 1){ 
     if ([_selectedSection isEqualToString:@"captures"]) { 
      if ([_capturesArray count] > 0) { 
       // Has Captures 
       CaptureTableViewCells *capCell = (CaptureTableViewCells *)cell; 

       // Get Capture At Index 
       Capture *capture = _capturesArray[indexPath.row]; 

       capCell.capture = capture; 

       NSURLRequest *request = [NSURLRequest requestWithURL:capture.captureImage.imageURL]; 
       UIImage *placeholderImage = [UIImage imageNamed:@"Overlay"]; 

       __weak CaptureTableViewCells *weakCell = capCell; 

       [capCell.captureImageView setImageWithURLRequest:request 
              placeholderImage:placeholderImage 
                 success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { 

                  weakCell.captureImageView.image = image; 
                  weakCell.capture.captureImage.image = weakCell.captureImageView.image; 
                  [weakCell.activityIndicator stopAnimating]; 
                  [weakCell setNeedsLayout]; 

                 } failure:nil]; 
      } else { 
       // No Captures 
       UILabel *label = (UILabel *)[cell viewWithTag:1]; 
       label.text = @"You have no Captures."; 
      } 
     } else if ([_selectedSection isEqualToString:@"comments"]) { 
      if ([_commentsArray count] > 0) { 
       // Has Captures 
      } else { 
       // No Captures 
       UILabel *label = (UILabel *)[cell viewWithTag:1]; 
       label.text = @"You have no comments."; 
      } 
     } else if ([_selectedSection isEqualToString:@"followers"]) { 
      if ([_followersArray count] > 0) { 
       // Has Captures 
      } else { 
       // No Captures 
       UILabel *label = (UILabel *)[cell viewWithTag:1]; 
       label.text = @"You have no followers."; 
      } 
     } else if ([_selectedSection isEqualToString:@"following"]) { 
      if ([_followingArray count] > 0) { 
       // Has Captures 
      } else { 
       // No Captures 
       UILabel *label = (UILabel *)[cell viewWithTag:1]; 
       label.text = @"You are not following anyone."; 
      } 
     } 
    } 

    // Remove seperator inset 
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { 
     [cell setSeparatorInset:UIEdgeInsetsZero]; 
    } 

    // Prevent the cell from inheriting the Table View's margin settings 
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) { 
     [cell setPreservesSuperviewLayoutMargins:NO]; 
    } 

    // Explictly set your cell's layout margins 
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) { 
     [cell setLayoutMargins:UIEdgeInsetsZero]; 
    } 

    // Setup Style For Cell 
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    if (indexPath == [NSIndexPath indexPathForRow:0 inSection:0]) { 
     return 208; 
    } else if (indexPath == [NSIndexPath indexPathForRow:1 inSection:0]) { 
     return 72; 
    } else if (indexPath.section == 1){ 
     if ([_selectedSection isEqualToString:@"captures"]) { 
      if ([_capturesArray count] > 0) { 
       Capture *capture = _capturesArray[indexPath.row]; 

       return capture.cellHeight; 
      } else { 
       return self.view.frame.size.height - 280 - self.navigationController.navigationBar.frame.size.height - self.tabBarController.tabBar.frame.size.height; 
      } 
     } else if ([_selectedSection isEqualToString:@"comments"]) { 
      if ([_commentsArray count] > 0) { 
       Capture *capture = _commentsArray[indexPath.row]; 

       return capture.cellHeight; 
      } else { 
       return self.view.frame.size.height - 280 - self.navigationController.navigationBar.frame.size.height - self.tabBarController.tabBar.frame.size.height; 
      } 
     } else if ([_selectedSection isEqualToString:@"followers"]) { 
      if ([_followersArray count] > 0) { 
       return 0; 
      } else { 
       return self.view.frame.size.height - 280 - self.navigationController.navigationBar.frame.size.height - self.tabBarController.tabBar.frame.size.height; 
      } 
     } else if ([_selectedSection isEqualToString:@"following"]) { 
      if ([_followingArray count] > 0) { 
       return 0; 
      } else { 
       return self.view.frame.size.height - 280 - self.navigationController.navigationBar.frame.size.height - self.tabBarController.tabBar.frame.size.height; 
      } 
     } 
    } 
    return 0; 
} 

UPDATE 所以我注释掉中的tableView一切:heightForRowAtIndexPath:和刚刚返回100中每个单元的高度。现在我的Section 0单元格可见。当我添加日志语句以查看发生了什么这是我得到的。

这是我的代码:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
     NSLog(@"IndexPath: %ld, %ld", (long)indexPath.section, (long)indexPath.row); 
     if (indexPath == [NSIndexPath indexPathForRow:0 inSection:0]) { 
      NSLog(@"Setting Height"); 
      return 208; 
     } 

这是我的日志:

2015-12-14 13:27:21.678 Captures[324:42956] IndexPath: 0, 0 
2015-12-14 13:27:21.678 Captures[324:42956] IndexPath: 0, 1 

正如你可以看到它跳过就在设置第0行0的高度在我的iPhone 6 iPhone5s没有发生。有谁知道为什么会发生这种情况?

+0

你是否检查过视图调试器,如果该部分转移到你不能看到它的顶部? –

+0

您是在故事板或代码中创建表格视图吗?你在使用自动布局吗? – Adeel

+0

@ Mr.T我可以在屏幕顶部看到我的Section 0标题,并且没有任何内容。它只是跳到第1部分。我不太熟悉视图调试器。我做了调试 - >查看调试 - >捕获视图层次结构。一旦它被打开,我点击“显示剪辑内容”,并据我所知,没有什么比我的Section 0标题更重要。 –

回答

0

我创建了一个小测试应用程序来确认:您不应该使用==运算符来比较NSIndexPath对象,例如您在代码中。虽然它似乎在我的iPhone 6模拟器上工作,但它不适用于iPhone 5模拟器。

您应该使用compare:方法或检查NSIndexPath的各个属性,而不是检查2个对象是否相等。

每苹果的文档,compare:似乎是首选:Comparing Index Paths

if([indexPath compare:[NSIndexPath indexPathForItem:0 inSection:0]] == NSOrderedSame) 
    // do something 

您还可以检查NSIndexPath的各个属性,如果你不必在意这两个sectionrow性能。

if(indexPath.row == 0 && indexPath.section == 0) 
    // do something 
+0

谢谢@ Stonz2我做了你所建议的改变,它效果很好。 –