如何使用 RxSwift 在 1 行中显示 3 列集合视图
How to show collection view 3 columns in 1 row with RxSwift
这是我的代码:
import UIKit
import RxSwift
import RxCocoa
import RxOptional
class ViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
let disposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let items = Observable.just(
(0..<20).map{ "Test \([=10=])" }
)
items.asObservable().bindTo(self.collectionView.rx.items(cellIdentifier: CustomCollectionViewCell.reuseIdentifier, cellType: CustomCollectionViewCell.self)) { row, data, cell in
cell.data = data
}.addDisposableTo(disposeBag)
}
}
那么如何在 1 行中显示 3 列。
我找不到任何关于 RxSwift 集合视图的教程。
像这样:
class ViewController: UIViewController, UICollectionViewDelegateFlowLayout {
....
override func viewDidLoad() {
super.viewDidLoad()
...
items.asObservable().bindTo(self.collectionView.rx.items(cellIdentifier: CustomCollectionViewCell.reuseIdentifier, cellType: CustomCollectionViewCell.self)) { row, data, cell in
cell.data = data
}.addDisposableTo(disposeBag)
// add this line you can provide the cell size from delegate method
collectionView.rx.setDelegate(self).addDisposableTo(disposeBag)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = collectionView.bounds.width
let cellWidth = (width - 30) / 3 // compute your cell width
return CGSize(width: cellWidth, height: cellWidth / 0.6)
}
}
另一种选择:
let flowLayout = UICollectionViewFlowLayout()
let size = (collectionView.frame.size.width - CGFloat(30)) / CGFloat(3)
flowLayout.itemSize = CGSize(width: size, height: size)
collectionView.setCollectionViewLayout(flowLayout, animated: true)
这是我的代码:
import UIKit
import RxSwift
import RxCocoa
import RxOptional
class ViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
let disposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let items = Observable.just(
(0..<20).map{ "Test \([=10=])" }
)
items.asObservable().bindTo(self.collectionView.rx.items(cellIdentifier: CustomCollectionViewCell.reuseIdentifier, cellType: CustomCollectionViewCell.self)) { row, data, cell in
cell.data = data
}.addDisposableTo(disposeBag)
}
}
那么如何在 1 行中显示 3 列。
我找不到任何关于 RxSwift 集合视图的教程。
像这样:
class ViewController: UIViewController, UICollectionViewDelegateFlowLayout {
....
override func viewDidLoad() {
super.viewDidLoad()
...
items.asObservable().bindTo(self.collectionView.rx.items(cellIdentifier: CustomCollectionViewCell.reuseIdentifier, cellType: CustomCollectionViewCell.self)) { row, data, cell in
cell.data = data
}.addDisposableTo(disposeBag)
// add this line you can provide the cell size from delegate method
collectionView.rx.setDelegate(self).addDisposableTo(disposeBag)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = collectionView.bounds.width
let cellWidth = (width - 30) / 3 // compute your cell width
return CGSize(width: cellWidth, height: cellWidth / 0.6)
}
}
另一种选择:
let flowLayout = UICollectionViewFlowLayout()
let size = (collectionView.frame.size.width - CGFloat(30)) / CGFloat(3)
flowLayout.itemSize = CGSize(width: size, height: size)
collectionView.setCollectionViewLayout(flowLayout, animated: true)