reb*_*a87 2 ios uicollectionview uicollectionreusableview swift
我想用标题创建UICollectionView.我在mainStoryBoard上设置了Collection Reusable View,但是设备上没有显示任何内容.我试图搜索,但无法找出它为什么没有出现.一世
主要故事板
在设备上
导入UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var collectionView: UICollectionView!
var images = ["medal1","medal2","medal3","medal4","medal5","medal6","medal7","medal8","medal9","medal10","medal1","medal2","medal3","medal14"]
var texts = ["hi","yes","hoo","such","hi","yes","hoo","such","hi","yes","hoo","such","hi","yes"]
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
}
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCell
cell.myImage.image = UIImage(named: images[indexPath.row])
cell.achievementLabel.text = texts[indexPath.row]
return cell
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
}
import UIKit
Run Code Online (Sandbox Code Playgroud)
集合类查看类CustomCell:UICollectionViewCell {
@IBOutlet weak var myImage: UIImageView!
@IBOutlet weak var achievementLabel: UILabel!
}
Run Code Online (Sandbox Code Playgroud)
Collection Reusable View的类导入UIKit
class CollectionReusableView: UICollectionReusableView {
@IBOutlet weak var reuseableVimage: UIImageView!
}
> import UIKit
class ViewController: UIViewController, UICollectionViewDelegate {
@IBOutlet weak var collectionView: UICollectionView!
var images = ["medal1","medal2","medal3","medal4","medal5","medal6","medal7","medal8","medal9","medal10","medal1","medal2","medal3","medal14"]
var texts = ["hi","yes","hoo","such","hi","yes","hoo","such","hi","yes","hoo","such","hi","yes"]
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if kind == UICollectionElementKindSectionHeader {
let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "HeaderView", for: indexPath)
// do any programmatic customization, if any, here
return view
}
fatalError("Unexpected kind")
}
}
Run Code Online (Sandbox Code Playgroud)
你必须实现viewForSupplementaryElementOfKind:
酌情实施collectionView(_:viewForSupplementaryElementOfKind:at:),UICollectionElementKindSectionHeader或UICollectionElementKindSectionFooter适用.
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if kind == UICollectionElementKindSectionHeader {
let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "CollectionReusableView", for: indexPath) as! CollectionReusableView
// do any programmatic customization, if any, here
return view
}
fatalError("Unexpected kind")
}
Run Code Online (Sandbox Code Playgroud)确保IB中的标题可重用视图具有
在IB中,确保集合视图的"附件"具有下一个"Section Header"和"Section Footer"的复选标记.
| 归档时间: |
|
| 查看次数: |
3041 次 |
| 最近记录: |