- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试以这种方式创建提要布局
[-------2/3-------][---1/3---] /* 2 items - 2 thirds and 1 third */
[---1/3---][-------2/3-------] /* 2 items - 1 third and 2 thirds */
[-------2/3-------][---1/3---] /* 2 items - 2 thirds and 1 third */
[---1/3---][-------2/3-------] /* 2 items - 1 third and 2 thirds */
我可以通过做类似的事情来实现这一目标
class FeedSectionController: ListSectionController {
var entry: FeedEntry!
override init() {
super.init()
inset = UIEdgeInsets(top: 0, left: 0, bottom: 15, right: 0)
}
override func numberOfItems() -> Int {
return 2
}
override func sizeForItem(at index: Int) -> CGSize {
guard let ctx = collectionContext else { return .zero }
if index == 0 {
return CGSize(width: ((ctx.containerSize.width / 3) * 2), height: 30)
} else {
return CGSize(width: (ctx.containerSize.width / 3), height: 30)
}
}
override func cellForItem(at index: Int) -> UICollectionViewCell {
let cell = UICollectionViewCell()
cell.backgroundColor = index % 2 == 0 ? .lightGray : .darkGray
return cell
}
override func didUpdate(to object: Any) {
entry = object as? FeedEntry
}
}
但是结果实际上更像
[-------2/3-------][---1/3---] /* 2 items - 2 thirds and 1 third */
[-------2/3-------][---1/3---] /* 2 items - 2 thirds and 1 third */
[-------2/3-------][---1/3---] /* 2 items - 2 thirds and 1 third */
[-------2/3-------][---1/3---] /* 2 items - 2 thirds and 1 third */
这不太正确,这也意味着我每个单元格每个部分有 2 个完整的 feed 项目。
我想要的是每个部分都包含 X 数量的单元格,这些单元格基本上构建了一个 feed 项目。
但是,为了做到这一点,我需要按照前面概述的方式布局 FeedSectionController
,而不是 FeedSectionController
中的项目(我认为)。
最佳答案
我并不是 100% 熟悉 IGListKit
,但您可以仅使用标准 UICollectionView
来创建该效果
class FeedViewController: UIViewController {
private enum FeedItemWidth {
case oneThird, twoThirds
}
private lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "UICollectionViewCell")
cv.backgroundColor = .gray
cv.translatesAutoresizingMaskIntoConstraints = false
return cv
}()
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
if let collectionViewLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
collectionViewLayout.minimumInteritemSpacing = 0
collectionViewLayout.sectionInset = .init(top: 8, left: 8, bottom: 8, right: 8)
}
view.backgroundColor = .lightGray
view.addSubview(collectionView)
NSLayoutConstraint.activate([
collectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
collectionView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
collectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
collectionView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor)
])
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
collectionView.collectionViewLayout.invalidateLayout()
}
private func feedItemWidth(_ type: FeedItemWidth) -> CGFloat {
switch type {
case .oneThird:
return (collectionView.bounds.width / 3) - 12
case .twoThirds:
return ((collectionView.bounds.width / 3) * 2) - 12
}
}
}
extension FeedViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let layoutOptions = [
CGSize(width: feedItemWidth(.oneThird), height: 100),
CGSize(width: feedItemWidth(.twoThirds), height: 100),
CGSize(width: feedItemWidth(.twoThirds), height: 100),
CGSize(width: feedItemWidth(.oneThird), height: 100)
]
return layoutOptions[indexPath.item % layoutOptions.count]
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 8
}
}
extension FeedViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 35
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "UICollectionViewCell", for: indexPath)
cell.backgroundColor = (indexPath.item % 2 == 0) ? .white : .black
return cell
}
}
关于swift - 使用 IGListKit 或 UICollectionView 每行多个部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58547617/
使用 IGListkit 并且对抓取数据的职责有点困惑。 虚拟示例 - 假设我正在处理车辆类别,我想让一个部分 Controller 基本上处理某个类别的汽车品牌。 我的 View Controlle
我正在尝试使用 IGListKit 实现 Collection View 。它可以有一个或三个部分。数据源是实时填充的,因此需要在有新数据可用时刷新内容。 为此,我创建了一个符合 IGListDiff
我正在尝试在 iOS 中使用 IGListKit ( Link ) 来呈现自定义 Collection View 和 TableView ,其中我在部分 Controller 中有一个水平 Scrol
我正在构建一个严重依赖 Collection View 的应用程序,因此我决定使用 IGListKit。这是我必须做的: 这是我所拥有的: ...而且它没有朝任何方向移动! 这是我的代码: impor
我正在尝试滚动到使用 IGListKit 实现的 UICollectionView 的底部。我这样做的方法: func scrollToBottom(animated: Bool = true) {
我有一个部分 Controller “mySectionController”里面有3个不同的细胞 一个 B C 我只想在单元格 B 和 C 之间应用 .minimumLineSpacing 一个 B
所以我有这个模型 class Event: NSObject { var _eventName: String! var _venueName : String! var _e
我正在使用 IGListStackedSectionController,我想知道如何将多个对象传递给任何给定的 child 。 我有这样一个场景: let sectionController = I
我无法使用 didHighlightItem 和 didUnhighlightItem 函数在单击单元格时正确设置动画,因为我无法访问单元格的节号。我想知道我是否错误地解决了问题,或者是否有一种方法可
我正在尝试以这种方式创建提要布局 [-------2/3-------][---1/3---] /* 2 items - 2 thirds and 1 third */ [---1/3---][---
如何在 View 模型中有值时添加单元格,而在没有值时不添加单元格?我的意思是,我正在使用 ListBindingSectionController,并且对于每种单元格类型(图像、标题等),我都有单独
我找不到使用 IGListKit 3.1.1 来管理水平滚动 UICollectionView 上项目之间间距的方法。 我尝试了不同的方法... 使用UICollectionViewFlowLayou
我需要更改 Collection View (UICollectionView)的frame和顶部内容插图(contentInset.top)。顶部插图和框架的更改取决于 super View 的边界
IGListKit 库链接:https://github.com/Instagram/IGListKit 我正在使用 IGListKit 制作不同的截面单元格。我成功地完成了这项任务,但我无法使用 i
我正在使用 IGListKit 库(IGListKit)。我想创建一个两列的 Collection View 列表,如下所示。我已经阅读了 IGListKit 手册,但我无法理解如何实现这一目标。 我
我是一名优秀的程序员,十分优秀!