iPhone 5 UITableView 不显示

Posted

技术标签:

【中文标题】iPhone 5 UITableView 不显示【英文标题】:iPhone 5 UITableView not displaying 【发布时间】:2015-12-14 16:43:43 【问题描述】:

我目前正在开发一个包含许多 UITableViews 的应用程序,我通过在 iPhone 6 上进行测试来创建该应用程序。我最近开始在其他设备上进行测试,我注意到在 iPhone 5 上任何具有多个部分的 UITableView 都会跳过第 0 节,根本不显示它。虽然在我的 iPhone 6 和 iPhone 5s 上它会显示得非常好。下面是一些用于显示我的个人资料部分的 UITableView 的代码。任何帮助将不胜感激。值得一提的是,所有设备均运行 9.1。谢谢。

#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;

更新 所以我注释掉了 tableView: heightForRowAtIndexPath: 中的所有内容,并为每个单元格的高度返回了 100。现在我的第 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 上,这不会发生。有谁知道为什么会这样?

【问题讨论】:

您是否检查了视图调试器,如果该部分移动到您看不到的最顶部? 您是在情节提要还是在代码中创建表格视图?你在使用自动布局吗? @Mr.T 我可以在屏幕顶部看到我的第 0 节标题,下面什么都没有。它只是跳到第 1 节。我对视图调试器不太熟悉。我做了调试->查看调试->捕获视图层次结构。打开后,我单击“显示剪辑的内容”,据我所知,第 0 节标题上方没有任何内容。 @AdeelMiraj 我正在使用带有自动布局的情节提要。 您是否尝试记录每个单元格的高度? 【参考方案1】:

我创建了一个小型测试应用来确认:您不应该像在代码中那样使用== 运算符来比较NSIndexPathobjects。虽然它似乎在我的 iPhone 6 模拟器上运行,但它在 iPhone 5 模拟器上不运行。

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

根据 Apple 的文档,compare: 似乎是首选:Comparing Index Paths

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

如果您不一定关心sectionrow 属性,也可以检查NSIndexPath 的各个属性。

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

【讨论】:

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

以上是关于iPhone 5 UITableView 不显示的主要内容,如果未能解决你的问题,请参考以下文章

将 UITableView 大小调整为 iPhone 5/5s

UITableView 单元格不显示正确的详细信息

在 UITableView iPhone 中显示 MySQL 数据库中的图像

移动到显示 UITableView 的弹出窗口中的下一个视图

UITableView 与摇一摇和播放

iPhone - 帮助在 UITableView 中添加 9 个不同的部分