【问题标题】:How to return the correct heightForRowAtIndexPath如何返回正确的 heightForRowAtIndexPath
【发布时间】:2014-10-06 22:14:28
【问题描述】:

现在我正在尝试适应两款新的 iPhone 6 / Plus。我正在使用 UITableViewController 来实现我的表格。

在我的 Cell 内,我有一个 320x189 的 UImageView,几乎使用了整个单元格并充当每个单元格的背景。

我获得的动态图像和大小会有所不同,因此我无法控制图像的大小。通过使用约束,我能够调整整个细胞的图像。但我觉得 iPhone 6 Plus 的单元格高度应该至少高出 20px。

有什么方法可以检测到它的 iPhone 6 Plus/6 并在 heightForRowAtIndexPath 方法上覆盖返回的高度? O 也许我应该为 iPhone 6/ Plus 使用不同的 StoryBoard?

请指教。

谢谢

【问题讨论】:

    标签: objective-c iphone xcode uitableview ios8


    【解决方案1】:

    您可以根据屏幕高度检测它是什么手机。这些是 iPhone 4 和 5 的屏幕尺寸:http://www.idev101.com/code/User_Interface/sizes.html,这些是 iPhone 6/6+ 的尺寸:http://www.paintcodeapp.com/news/iphone-6-screens-demystified

    iPhone 4 高度为 480 pts,iPhone 5 高度为 568 pts,iPhone 6 高度为 667 pts,6+ 高度为 736 pts。

    您可以通过[UIScreen mainScreen].bounds.size.height获取设备屏幕的高度。

    这样您就可以知道您使用的是哪种手机:

    CGFloat h = [UIScreen mainScreen].bounds.size.height;
    if (h > 700){
        NSLog(@"iPhone 6+");
    }
    else if (h > 600){
        NSLog(@"iPhone 6");
    }
    else if (h > 500){
        NSLog(@"iPhone 5");
    }
    else{
        NSLog(@"iPhone 4");
    }
    

    至于表格单元格的高度,您可以在heightForRowAtIndexPath 方法中以编程方式将高度设置为图像的大小。如果您将图像放在某种数组中,则可以这样做:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return images[indexPath.row].size.height;
    }
    

    应该调整每个单元格的高度以适合其对应的图像。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 2016-01-18
      • 2020-06-11
      相关资源
      最近更新 更多