gpt4 book ai didi

ios - Swift UICollectionView 水平滚动不起作用

转载 作者:可可西里 更新时间:2023-11-01 01:32:55 26 4
gpt4 key购买 nike

我对不想水平滚动的 UICollectionView 有疑问。我想显示 5 个可以滚动的单元格。是什么阻止了我的 collectionview 滚动?

import UIKit

class FeaturedCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout
{
// Attributes
lazy var featuredVideos = UICollectionView(frame: .zero)

// Superclass initializer
required init?(coder aDecoder: NSCoder)
{
fatalError("init(coder:) has not been implemented")
}

// Custom initializer
required override init(frame: CGRect)
{
super.init(frame: frame)

let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .Horizontal
featuredVideos = UICollectionView(frame: self.frame, collectionViewLayout: layout)
featuredVideos.dataSource = self
featuredVideos.delegate = self

// Setting the collection view's scrolling behaviour
featuredVideos.pagingEnabled = true
featuredVideos.scrollEnabled = true
featuredVideos.setContentOffset(CGPoint(x: 0,y: 0), animated: true)

featuredVideos.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellId")

addSubview(featuredVideos)
setConstraints("H:|[v0(\(frame.width))]|", subviews: featuredVideos)
setConstraints("V:|[v0(345)]", subviews: featuredVideos)
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cellId", forIndexPath: indexPath)
if indexPath.item == 1 { cell.backgroundColor = .lightGrayColor() } else { cell.backgroundColor = .brownColor() }
return cell
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
return CGSizeMake(frame.width/3, frame.height)
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat
{
return 10
}
}

simulator view

编辑:UICollectionView 实际上不会对任何交互使用react,我尝试了“didSelectAtIndexPath”,但没有触发。

最佳答案

要在 UICollectionViewCell 中使用 UICollectionView 实现 UICollectionView 试试这个想法(两个 collectionViews 都是可滚动的):

CollectionViewController.swift

import UIKit

class CollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout
{
var featuredVideos: UICollectionView?

override func viewDidLoad() {

let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .Horizontal
featuredVideos = UICollectionView(frame: UIScreen.mainScreen().bounds, collectionViewLayout: layout)

featuredVideos!.dataSource = self
featuredVideos!.delegate = self

// Setting the collection view's scrolling behaviour
featuredVideos!.pagingEnabled = true
featuredVideos!.scrollEnabled = true
featuredVideos!.setContentOffset(CGPoint(x: 0,y: 0), animated: true)

featuredVideos!.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier: "cellId")
view.addSubview(featuredVideos!)

}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cellId", forIndexPath: indexPath) as! CollectionViewCell
cell.initCell()
if indexPath.item%2 == 0
{
cell.backgroundColor = .lightGrayColor()
}
else
{
cell.backgroundColor = .brownColor()
}
return cell
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
return CGSizeMake(300, UIScreen.mainScreen().bounds.height)
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat
{
return 10
}
}

CollectionViewCell.swift

class CollectionViewCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate
{
var collectionView: UICollectionView?

func initCell () {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .Horizontal
var collectionViewBounds = self.bounds
collectionViewBounds.size.height -= 80
collectionViewBounds.origin.y = 40
collectionView = UICollectionView(frame: collectionViewBounds, collectionViewLayout: layout)

collectionView!.dataSource = self
collectionView!.delegate = self

// Setting the collection view's scrolling behaviour
collectionView!.pagingEnabled = true
collectionView!.scrollEnabled = true
collectionView!.setContentOffset(CGPoint(x: 0,y: 0), animated: true)

collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellWithCollectionView")
addSubview(collectionView!)

}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cellWithCollectionView", forIndexPath: indexPath)
if indexPath.item%2 == 0
{
cell.backgroundColor = .blueColor()
}
else
{
cell.backgroundColor = .whiteColor()
}
return cell
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
return CGSizeMake(100, collectionView.frame.height)
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat
{
return 10
}

}

关于ios - Swift UICollectionView 水平滚动不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38976624/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com