Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在单个屏幕中有一些UITableView和一些UICollectionView_Ios_Swift_Uitableview_Uiviewcontroller_Uicollectionview - Fatal编程技术网

Ios 在单个屏幕中有一些UITableView和一些UICollectionView

Ios 在单个屏幕中有一些UITableView和一些UICollectionView,ios,swift,uitableview,uiviewcontroller,uicollectionview,Ios,Swift,Uitableview,Uiviewcontroller,Uicollectionview,我目前正在处理一个特殊的问题,我必须在一个屏幕中添加一个UITableView和两个UICollectionView以及两个标签 这是模型:- 现在,我的视图是这样的(我现在正在处理UI):- 下面的“活现在”和“相关故事”的UICLoDeNeVIEW是水平滚动的,并且在那些UICRealDebug视图的中间是UpababVIEW 不必在我构建的UIViewController类中压缩这些子视图,我希望以这样一种方式重新制作它,即整个视图可以滚动,同时保持视图中当前设置的子视图的相同滚动体

我目前正在处理一个特殊的问题,我必须在一个屏幕中添加一个UITableView和两个UICollectionView以及两个标签

这是模型:-

现在,我的视图是这样的(我现在正在处理UI):-

下面的“活现在”和“相关故事”的UICLoDeNeVIEW是水平滚动的,并且在那些UICRealDebug视图的中间是UpababVIEW

不必在我构建的UIViewController类中压缩这些子视图,我希望以这样一种方式重新制作它,即整个视图可以滚动,同时保持视图中当前设置的子视图的相同滚动体验

我曾考虑使用另一个包含所有视图(集合视图和表视图)的UITableview,但是,滚动该特定表视图会导致我必须添加的表视图出现不好的滚动体验

以上可以说是使用UIScrollView(将添加的UICollectionViews没有问题,因为它们是水平滚动的)

最好使用UICollectionView吗

任何有帮助的建议都是欢迎的。如果我这边有什么事,一定要告诉我

以下是我的源代码:

import UIKit
import SnapKit
import EasyPeasy

class ArticleViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UITableViewDelegate, UITableViewDataSource {

var liveLabel = UILabel()
var engageLabel = UILabel()
var storyLabel = UILabel()

var livelayout = UICollectionViewFlowLayout.init()
var storylayout = UICollectionViewFlowLayout.init()
var liveCollectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: UICollectionViewFlowLayout.init())
var storyCollectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: UICollectionViewFlowLayout.init())

var liveRows = [
    "https://images.sportsflashes.com/australia-win-first-t20i-after-nail-biting-finish-at-vizag1551028137.jpg",
    "https://images.sportsflashes.com/australia-win-first-t20i-after-nail-biting-finish-at-vizag1551028137.jpg",
    "https://images.sportsflashes.com/australia-win-first-t20i-after-nail-biting-finish-at-vizag1551028137.jpg",
    "https://images.sportsflashes.com/australia-win-first-t20i-after-nail-biting-finish-at-vizag1551028137.jpg",
    "https://images.sportsflashes.com/australia-win-first-t20i-after-nail-biting-finish-at-vizag1551028137.jpg",
    "https://images.sportsflashes.com/australia-win-first-t20i-after-nail-biting-finish-at-vizag1551028137.jpg",
    "https://images.sportsflashes.com/australia-win-first-t20i-after-nail-biting-finish-at-vizag1551028137.jpg"
]

var articleRows = [
    "https://c.ndtvimg.com/2019-03/6dkbrsrg_varun-dhawan_625x300_14_March_19.jpg",
    "https://c.ndtvimg.com/2019-03/6dkbrsrg_varun-dhawan_625x300_14_March_19.jpg",
    "https://c.ndtvimg.com/2019-03/6dkbrsrg_varun-dhawan_625x300_14_March_19.jpg",
    "https://c.ndtvimg.com/2019-03/6dkbrsrg_varun-dhawan_625x300_14_March_19.jpg",
    "https://c.ndtvimg.com/2019-03/6dkbrsrg_varun-dhawan_625x300_14_March_19.jpg",
    "https://c.ndtvimg.com/2019-03/6dkbrsrg_varun-dhawan_625x300_14_March_19.jpg",
    "https://c.ndtvimg.com/2019-03/6dkbrsrg_varun-dhawan_625x300_14_March_19.jpg",
    "https://c.ndtvimg.com/2019-03/6dkbrsrg_varun-dhawan_625x300_14_March_19.jpg",
    "https://c.ndtvimg.com/2019-03/6dkbrsrg_varun-dhawan_625x300_14_March_19.jpg"
]

var storyRows = [
    "https://www.anime-planet.com/images/characters/takuma-mamizuka-83947.jpg",
    "https://www.anime-planet.com/images/characters/takuma-mamizuka-83947.jpg",
    "https://www.anime-planet.com/images/characters/takuma-mamizuka-83947.jpg",
    "https://www.anime-planet.com/images/characters/takuma-mamizuka-83947.jpg",
    "https://www.anime-planet.com/images/characters/takuma-mamizuka-83947.jpg",
    "https://www.anime-planet.com/images/characters/takuma-mamizuka-83947.jpg",
    "https://www.anime-planet.com/images/characters/takuma-mamizuka-83947.jpg",
    "https://www.anime-planet.com/images/characters/takuma-mamizuka-83947.jpg",
    "https://www.anime-planet.com/images/characters/takuma-mamizuka-83947.jpg"
]

let table = UITableView()

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.view.backgroundColor = UIColor.white

    if self.navigationController == nil {
        return
    }

    self.navigationController?.isNavigationBarHidden = false
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(image:(UIImage(named: "back_arrow")?.withRenderingMode(.alwaysOriginal)), style:.plain, target:self, action:#selector(backPress))

    var dateBarButtonItem = UIBarButtonItem(title: "Mar 2019", style: .plain, target: self, action: nil)
    dateBarButtonItem.tintColor = UIColor.black
    self.navigationItem.rightBarButtonItem = dateBarButtonItem

    // Create a navView to add to the navigation bar
    let navView = UIView()

    // Create the label
    let nameLabel = UILabel()
    nameLabel.text = "Pavan Vasan"
    nameLabel.sizeToFit()
    nameLabel.center = navView.center
    nameLabel.textAlignment = NSTextAlignment.center

    // Create the image view
    let image = UIImageView()
    image.image = UIImage(named: "twitter")
    // To maintain the image's aspect ratio:
    let imageAspect = image.image!.size.width/image.image!.size.height
    // Setting the image frame so that it's immediately before the text:
    image.frame = CGRect(x: nameLabel.frame.origin.x-nameLabel.frame.size.height*imageAspect, y: nameLabel.frame.origin.y, width: nameLabel.frame.size.height*imageAspect, height: nameLabel.frame.size.height)
    image.contentMode = UIView.ContentMode.scaleAspectFit
    // Add both the label and image view to the navView
    navView.addSubview(nameLabel)
    navView.addSubview(image)

    // Set the navigation bar's navigation item's titleView to the navView
    self.navigationItem.titleView = navView

    // Set the navView's frame to fit within the titleView
    navView.sizeToFit()
}

override func viewDidLoad() {
    super.viewDidLoad()
    setupLiveCollectionView()
    setupTable()
    setupStoryCollectionView()
}

func setupLiveCollectionView() {
    self.view.addSubview(self.liveLabel)
    self.liveLabel.text = "Live now"
    self.liveLabel.font = UIFont.boldSystemFont(ofSize: 17.5)
    self.liveLabel.textColor = UIColor.black
    self.liveLabel.textAlignment = .center
    self.liveLabel.easy.layout(
        Left(10).to(self.view),
        Top(75).to(self.view)
    )

    livelayout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    livelayout.itemSize = CGSize(width: 75, height: 75)
    livelayout.scrollDirection = .horizontal

    liveCollectionView = UICollectionView(frame: CGRect(x: 0, y: 100, width: self.view.frame.width, height: 95), collectionViewLayout: livelayout)
    liveCollectionView.dataSource = self
    liveCollectionView.delegate = self
    liveCollectionView.register(LiveViewCell.self, forCellWithReuseIdentifier: "MyCell")
    liveCollectionView.backgroundColor = UIColor.white
    liveCollectionView.showsHorizontalScrollIndicator = false
    self.view.addSubview(liveCollectionView)
}

func setupStoryCollectionView() {
    self.view.addSubview(self.storyLabel)
    self.storyLabel.text = "Related Stories"
    self.storyLabel.font = UIFont.boldSystemFont(ofSize: 17.5)
    self.storyLabel.textColor = UIColor.black
    self.storyLabel.textAlignment = .center
    self.storyLabel.easy.layout(
        Left(10).to(self.view),
        Top(15).to(self.table)
    )

    storylayout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    storylayout.itemSize = CGSize(width: 120, height: 120)
    storylayout.scrollDirection = .horizontal

    storyCollectionView = UICollectionView(frame: CGRect(x: 0, y: 250 + self.view.bounds.height*0.40, width: self.view.frame.width, height: 130), collectionViewLayout: storylayout)
    storyCollectionView.dataSource = self
    storyCollectionView.delegate = self
    storyCollectionView.register(StoryViewCell.self, forCellWithReuseIdentifier: "StoryCell")
    storyCollectionView.backgroundColor = UIColor.white
    storyCollectionView.showsHorizontalScrollIndicator = false
    self.view.addSubview(storyCollectionView)
}

func setupTable() {

    table.delegate = self
    table.dataSource = self
    table.register(ArticleTableViewCell.self, forCellReuseIdentifier: "ArticleCell")
    table.separatorStyle = .none

    self.view.addSubview(table)
    self.table.easy.layout(
        Top(15).to(self.liveCollectionView),
        Left(0).to(self.view),
        Width(self.view.bounds.width),
        Height(self.view.bounds.height*0.4)
    )
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if collectionView == self.liveCollectionView {
        return self.liveRows.count
    } else if collectionView == self.storyCollectionView {
        return self.storyRows.count
    }
    return 0
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if collectionView == self.liveCollectionView {
        let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath as IndexPath) as! LiveViewCell
        myCell.configure(self.liveRows[indexPath.row])
        return myCell
    } else if collectionView == self.storyCollectionView {
        let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "StoryCell", for: indexPath as IndexPath) as! StoryViewCell
        myCell.configure(self.storyRows[indexPath.row])
        myCell.layer.borderColor = UIColor.black.cgColor
        myCell.layer.cornerRadius = 15
        myCell.layer.borderWidth = 0.5
        myCell.layer.masksToBounds = true
        return myCell
    }
    return UICollectionViewCell()
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.articleRows.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ArticleCell", for: indexPath) as! ArticleTableViewCell
    cell.configure(self.articleRows[indexPath.row])
    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 325
}

@objc func backPress(sender:UIButton!) {
    self.navigationController?.popViewController(animated: true)
}
}

在复杂的屏幕中,最好只使用一种主列表视图类型(UITableView或UICollectionView)

在其中,您可以添加UIStackView或更多UITableView或UICollectionView


在您的情况下,我会使用UICollectionView并根据您的需要更改单元格的大小

在复杂屏幕中,最好只使用一种主列表视图类型(UITableView或UICollectionView)

在其中,您可以添加UIStackView或更多UITableView或UICollectionView


在您的情况下,我会使用UICollectionView并根据您的需要更改单元格的大小

您可以使用单个tableview进行全屏显示,并通过在其中添加
UICollectionView
来创建自定义单元格。

您可以使用单个tableview进行全屏显示,并通过添加
UICollectionView
在其中。

嵌套滚动视图可能违反HIG指南,该指南主张不要将滚动视图放置在另一个滚动视图中

事实上,与我们的用例类似,苹果给出了他们的股票应用程序示例,其中股票报价垂直滚动到公司特定信息上方,而公司特定信息水平滚动


有关更多详细信息,请参阅位于

的HIG文档。嵌套滚动视图可能违反HIG指南,该指南提倡不要将滚动视图放置在另一个滚动视图中

事实上,与我们的用例类似,苹果给出了他们的股票应用程序示例,其中股票报价垂直滚动到公司特定信息上方,而公司特定信息水平滚动


有关更多详细信息,请参阅位于

的HIG文档。您可以在更新下面的代码中不做任何重大更改

  • 首先,需要设置Tableview高度的约束出口。e、 g consTblHeight
  • 添加Tableview大小更改观察者
  • 观察者方法

  • 您可以在更新下面的代码中不做任何重大更改

  • 首先,需要设置Tableview高度的约束出口。e、 g consTblHeight
  • 添加Tableview大小更改观察者
  • 观察者方法

  • 将单个表格视图与自定义单元格一起使用-某些单元格仅包含
    UICollectionView
    。你可以在需要的地方获得一致的垂直滚动和独立的水平滚动。@Losiowaty:如果我必须接受你的方法,我还必须在主表视图中包含一个表视图。单元格中的子表视图(具有垂直滚动)是否会有滚动问题,因为父表视图也具有垂直滚动?如果我只拥有集合视图,那么故事会有所不同。为什么还有另一个表视图?只需使用该表视图中的单元格作为主表视图的单元格。@PavanVasan您可以在单个表视图中添加多个自定义单元格,并在其中的特定位置添加多个集合视图,也可以添加页眉或页脚。@Losiowaty,我来看看,谢谢。而且,你是对的。我不需要另一个表视图。谢谢你指出这一点。直到现在才意识到这一点使用带有自定义单元格的单个表视图-一些单元格将只包含
    UICollectionView
    。你可以在需要的地方获得一致的垂直滚动和独立的水平滚动。@Losiowaty:如果我必须接受你的方法,我还必须在主表视图中包含一个表视图。单元格中的子表视图(具有垂直滚动)是否会有滚动问题,因为父表视图也具有垂直滚动?如果我只拥有集合视图,那么故事会有所不同。为什么还有另一个表视图?只需使用该表视图中的单元格作为主表视图的单元格。@PavanVasan您可以在单个表视图中添加多个自定义单元格,并在其中的特定位置添加多个集合视图,也可以添加页眉或页脚。@Losiowaty,我来看看,谢谢。而且,你是对的。我不需要另一个表视图。谢谢你指出这一点。直到现在才意识到
    func setupTable() {
        table.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.new, context: nil)
    }
    
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
                self.consTblHeight.constant = self.table.contentSize.height
                self.view.layoutIfNeeded()
            }