我使用此代码来获取正确的类型,但没有获得我想要的 View ,任何人都可以告诉我哪里错了
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
screenSize = UIScreen.main.bounds
screenWidth = screenSize.width
screenHeight = screenSize.height
videosCollectionView.delegate = self
videosCollectionView.dataSource = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 4
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = videosCollectionView.dequeueReusableCell(withReuseIdentifier: "Videos", for: indexPath as IndexPath) as! VideosCollectionViewCell
cell.mainImgVw.image = logoImage[indexPath.row]
cell.durationLabel.text = "duration"
cell.nameLabel.text = "Test Video"
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let xPadding = 10
let spacing = 10
let rightPadding = 10
let width = (CGFloat(UIScreen.main.bounds.size.width) - CGFloat(xPadding + spacing + rightPadding))/2
let height = CGFloat(215)
return CGSize(width: width, height: height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(10, 10, 10, 10)
}
请告诉我哪里错了。请帮助我。
最佳答案
正如您所说,您想使用 collectionViewFlowDelegateLayout。因此,您必须将 Storyboard 中的所有值设置为 0,如下所示。
然后您必须在 View Controller 类中编写此代码。
该方法用于设置 Collection View 中的单元格大小。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let xPadding = 10
let spacing = 10
let rightPadding = 10
let width = (CGFloat(UIScreen.main.bounds.size.width) - CGFloat(xPadding + spacing + rightPadding))/2
let height = CGFloat(215)
return CGSize(width: width, height: height)
}
此方法用于将边距应用于指定部分的内容。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(10, 10, 10, 10)
}
此方法用于节中连续行或列之间的间距。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 10
}
关于ios - 如何使用collectionview流布局来获得正确的 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49937074/