简体   繁体   English

如何在 iPhone 的 list(tableView) 和 iPad 的 grid(collectionView) 上显示数据

[英]How to display the data on list(tableView) for iPhone and grid(collectionView) for iPad

I have a code assessment and never think about to something like this;我有一个代码评估,从来没有想过这样的事情; I have to display the data retrieved from an API in two different layouts :我必须以两种不同的布局显示从 API 检索的数据:

  • For iPad;适用于 iPad; only in landscape mode and in a grid.仅在横向模式和网格中。 I will do it with a UIcollectionView我会用 UIcollectionView 来做

  • For iPhone;对于 iPhone; only in portrait mode and in a list.仅在纵向模式和列表中。 I will do it with a UItableView or StackView.我会用 UItableView 或 StackView 来做。

The problem is how to implement the logic that populates the collection view or the table view from the view controller.问题是如何实现从视图控制器填充集合视图或表视图的逻辑。 I think I have to use the UITraitCollection but I know if its the better option.我想我必须使用 UITraitCollection 但我知道它是否是更好的选择。

Someone can help me?有人可以帮助我吗? Thanks谢谢

You can get the device from this method您可以通过此方法获取设备

// 1. request an UITraitCollection instance // 1. 请求一个 UITraitCollection 实例

  let deviceIdiom = UIScreen.main.traitCollection.userInterfaceIdiom

// 2. check the idiom // 2. 检查习语

  switch (deviceIdiom) {

 case .pad:
    print("iPad style UI")
 case .phone:
    print("iPhone and iPod touch style UI")
 case .tv: 
   print("tvOS style UI")
 default:
    print("Unspecified UI idiom")
}

And For Orientation use this method:-并且对于方向使用此方法:-

struct Orientation {
// indicate current device is in the LandScape orientation
  static var isLandscape: Bool {
    get {
        return UIDevice.current.orientation.isValidInterfaceOrientation
            ? UIDevice.current.orientation.isLandscape
            : UIApplication.shared.statusBarOrientation.isLandscape
    }
  }
// indicate current device is in the Portrait orientation
  static var isPortrait: Bool {
    get {
        return UIDevice.current.orientation.isValidInterfaceOrientation
            ? UIDevice.current.orientation.isPortrait
            : UIApplication.shared.statusBarOrientation.isPortrait
      }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM