2015-12-14 44 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我做了你所建議的改變,它效果很好。 –