如何使用故事板和 swift 在一个集合视图中设置每个集合视图单元格大小?
how to set each collectionview cell size in one collectionview using storyboard and swfit?
我在 viewController 中有一个 collectionView,在这个 collectionView 中我有两个 collectionView 单元格,每个单元格都有自己的单元格标识符和大小。
我要做什么,当用户点击 CardView 按钮时,collectionView 将显示标识符 "CardCell" 大小为 300x400 的单元格项目。
并且如果用户点击 ListView,它将显示标识符 "ListCell" 大小为 300x100 的单元格项目。
Currently I'm detecting which cell to show, I created enum and
variable cellIndex.
enum CellIndexType: Int {
case cardView
case listView
}
var cellIndex = 0
and If cellIndex equals to 0, vc will display "CardCell" and if 1,
will display "ListCell".
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var cell = Cell()
if cellIndex == CellIndexType.cardView.rawValue {
if let reusableCell = collectionView.dequeueReusableCell(withReuseIdentifier: "CardCell", for: indexPath) as? Cell {
cell = reusableCell
}
} else {
if let reusableCell = collectionView.dequeueReusableCell(withReuseIdentifier: "ListCardCell", for: indexPath) as? Cell {
cell = reusableCell
}
}
return cell
}
Here is my viewController with two cells in one CollectionView.
Here is how my viewController looks like.
Here is how collectionView's attribute inspector and size inspector
look like.
但是根据我在故事板和代码中的配置,它始终显示 300x400 的单元格大小。
我可以用什么方式实现这个逻辑?
首先,您必须在情节提要中将 collectionView 的单元格大小设置为 none。
其次,使用 collectionView 的 sizeForItemAt
func :
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if self.cellIndex == 0{
return CGSize(width: 300, height: 400)
} else if self.cellIndex == 1{
return CGSize(width: 300, height: 100)
}
}
此外,请在 viewDidLoad
注册您的手机:
yourCollectionView.delegate = self
yourCollectionView.dataSource = self
yourCollectionView.register(UINib(nibName: "CardCell", bundle: nil), forCellWithReuseIdentifier: "CardCell")
yourCollectionView.register(UINib(nibName: "ListCell", bundle: nil), forCellWithReuseIdentifier: "ListCell")
yourCollectionView.reloadData()
希望对您有所帮助...
我在 viewController 中有一个 collectionView,在这个 collectionView 中我有两个 collectionView 单元格,每个单元格都有自己的单元格标识符和大小。
我要做什么,当用户点击 CardView 按钮时,collectionView 将显示标识符 "CardCell" 大小为 300x400 的单元格项目。
并且如果用户点击 ListView,它将显示标识符 "ListCell" 大小为 300x100 的单元格项目。
Currently I'm detecting which cell to show, I created enum and variable cellIndex.
enum CellIndexType: Int {
case cardView
case listView
}
var cellIndex = 0
and If cellIndex equals to 0, vc will display "CardCell" and if 1, will display "ListCell".
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
var cell = Cell()
if cellIndex == CellIndexType.cardView.rawValue {
if let reusableCell = collectionView.dequeueReusableCell(withReuseIdentifier: "CardCell", for: indexPath) as? Cell {
cell = reusableCell
}
} else {
if let reusableCell = collectionView.dequeueReusableCell(withReuseIdentifier: "ListCardCell", for: indexPath) as? Cell {
cell = reusableCell
}
}
return cell
}
Here is my viewController with two cells in one CollectionView.
Here is how my viewController looks like.
Here is how collectionView's attribute inspector and size inspector look like.
但是根据我在故事板和代码中的配置,它始终显示 300x400 的单元格大小。
我可以用什么方式实现这个逻辑?
首先,您必须在情节提要中将 collectionView 的单元格大小设置为 none。
其次,使用 collectionView 的 sizeForItemAt
func :
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if self.cellIndex == 0{
return CGSize(width: 300, height: 400)
} else if self.cellIndex == 1{
return CGSize(width: 300, height: 100)
}
}
此外,请在 viewDidLoad
注册您的手机:
yourCollectionView.delegate = self
yourCollectionView.dataSource = self
yourCollectionView.register(UINib(nibName: "CardCell", bundle: nil), forCellWithReuseIdentifier: "CardCell")
yourCollectionView.register(UINib(nibName: "ListCell", bundle: nil), forCellWithReuseIdentifier: "ListCell")
yourCollectionView.reloadData()
希望对您有所帮助...