如何使用swift切换到交错集合视图和iOS中每行一项的列表视图?
我正在集合视图单元格中显示一些图像。到目前为止,我使用的是一个简单的集合视图,它显示项目,目前每行显示两个项目。以下代码如下所示:如何使用swift切换到交错集合视图和iOS中每行一项的列表视图?,ios,swift,xcode,uicollectionview,uikit,Ios,Swift,Xcode,Uicollectionview,Uikit,我正在集合视图单元格中显示一些图像。到目前为止,我使用的是一个简单的集合视图,它显示项目,目前每行显示两个项目。以下代码如下所示: import UIKit import MBProgressHUD class HotViewController: BaseViewController { // MARK: - IBOutlets @IBOutlet var collectionView: UICollectionView! @IBOutlet weak var f
import UIKit
import MBProgressHUD
class HotViewController: BaseViewController {
// MARK: - IBOutlets
@IBOutlet var collectionView: UICollectionView!
@IBOutlet weak var filterSwitch: UISwitch!
@IBOutlet weak var toolbarHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var toolbarView: UIView!
@IBOutlet weak var messageLabel: UILabel!
// MARK: - Properties
let hotViewModel = HotViewModel()
var hotPhotos = [ImageMeta]()
var filteredPhotos = [ImageMeta]()
// MARK: - ViewLifeCycle
override func viewDidLoad() {
super.viewDidLoad()
setupViewModel()
}
// MARK: - Methods
func setupViewModel() {
MBProgressHUD.showAdded(to: self.view, animated: true)
hotViewModel.getHotPhotos()
hotViewModel.getPhotoListDidSucess = { [weak self] list in
guard let strongSelf = self else {return}
strongSelf.hotPhotos = list
strongSelf.filteredPhotos = list.filter{$0.in_most_viral == true}
DispatchQueue.main.async {
MBProgressHUD.hide(for: strongSelf.view, animated: true)
strongSelf.collectionView.reloadData()
}
}
hotViewModel.getPhotoListDidFailed = { [weak self] message in
print("message \(message)")
guard let strongSelf = self else {return}
DispatchQueue.main.async {
MBProgressHUD.hide(for: strongSelf.view, animated: true)
}
}
}
// MARK: - Actions
@IBAction func switchValueDidChanged(_ sender: Any) {
self.collectionView.reloadData()
}
@IBAction func aboutButtonClicked(_ sender: UIButton) {
showPopUp()
}
}
// MARK: - CollectionViewDataSource
extension HotViewController: UICollectionViewDataSource,UICollectionViewDelegate {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if filterSwitch.isOn && filteredPhotos.count == 0 {
messageLabel.isHidden = false
}
else{
messageLabel.isHidden = true
}
return (filterSwitch.isOn ? filteredPhotos.count : hotPhotos.count)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let photo = filterSwitch.isOn ? filteredPhotos[indexPath.row] : hotPhotos[indexPath.row]
let cell : PhotoListCell = collectionView.dequeueReusableCell(for: indexPath)
cell.configureCell(with: photo)
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let photo = filterSwitch.isOn ? filteredPhotos[indexPath.row] : hotPhotos[indexPath.row]
showDetailedPage(metaData: photo)
}
}
// MARK: - UICollectionViewDelegateFlowLayout
extension HotViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return CGFloat(interitemSpacing)
}
func collectionView(_ collectionView: UICollectionView, layout
collectionViewLayout: UICollectionViewLayout,
minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return CGFloat(lineSpacing)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return getItemSize()
}
}
我得到以下结果:
所以在这张图中你可以看到1和2用红色标记。我想要的是
- A) 按1时,每行的项目数应变为1 而不是2
- B) 按下2键时,单元格应交错排列 而不是完美的正方形。
如何在单击按钮时切换到交错视图、列表视图和常规视图?是否可以添加标志,单击后只需重新加载collectionView。
完成集合视图代理中的所有代码…我知道我想获得交错视图布局,以及如何限制每行仅一项?请查看以下链接。这可能对你有帮助