ios - 在 UICollectionView 中动态插入 Item
问题描述
我正在处理需要UICollectionView
动态添加项目的要求。
这是我的代码ViewController
import UIKit
class ViewController: UIViewController {
enum Direction {
case Horizonatal
case Verticle
}
var enumDirection: Direction = .Verticle
var direction = "Verticle"
var SectionsAndRows = [Int]()
override func viewDidLoad() {
super.viewDidLoad()
SectionsAndRows.append(4)
SectionsAndRows.append(3)
SectionsAndRows.append(2)
SectionsAndRows.append(1)
//SectionsAndRows.append(5)
}
@IBOutlet var gridCollectionView: UICollectionView! {
didSet {
gridCollectionView.bounces = false
}
}
@IBOutlet var gridLayout: UICollectionViewFlowLayout! {
didSet {
//gridLayout.stickyRowsCount = 0
gridLayout.scrollDirection = .horizontal
//gridLayout.stickyColumnsCount = 0
gridLayout.minimumLineSpacing = 5
gridLayout.minimumInteritemSpacing = 5
}
}
}
// MARK: - Collection view data source and delegate methods
extension ViewController: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return SectionsAndRows.count
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
print(SectionsAndRows[section])
return SectionsAndRows[section]
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CollectionViewCell.reuseID, for: indexPath) as? CollectionViewCell else {
return UICollectionViewCell()
}
print("Current Section ==\(indexPath.section) CurrentRow ===\(indexPath.row) and its rows count ==\(SectionsAndRows[indexPath.section])")
cell.titleLabel.text = ""
cell.btn.addTarget(self, action: #selector(handleAdd(sender:)), for: .touchUpInside)
cell.btn.tag = (indexPath.section * 1000) + indexPath.row
if enumDirection == .Verticle {
if indexPath.section == SectionsAndRows.count - 1 {
cell.btn.setTitle("+", for: .normal)
} else {
cell.btn.setTitle("\(indexPath)", for: .normal)
}
}
return cell
}
@objc func handleAdd(sender: UIButton) {
// Perform some opration
}
}
extension ViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 5
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 100, height: 100)
}
}
CollectionViewCell.swift
import UIKit
class CollectionViewCell: UICollectionViewCell {
static let reuseID = "CollectionViewCell"
@IBOutlet weak var btn: UIButton!
@IBOutlet weak var titleLabel: UILabel!
}
如果您运行代码,它将在每行中显示一个包含 1 行和 1 列的集合。如果您取消注释viewDidLoad()
( SectionsAndRows.append(5)
) 函数的最后一行,则它可以正常工作。
我的观察是,最后一部分的CollectionView
列数最多。这是正确的还是 CollectionView 的错误?
解决方案
推荐阅读
- javascript - 哪种方式更方便在无限循环中执行异步函数,每次执行之间休眠(Node.js / Vanilla JS)
- r - 调用另一个.Rmd - R的字符编码UTF-8问题
- php - 使用未定义常量 PHP 7.2
- linux - Bash 减法故障:var=$((var-1)) 给出荒谬的值
- powershell - Powershell 注册表项
- selenium - 使用 ClueCumber 报告插件重新运行黄瓜场景
- linux - 为文件夹中的每个文件运行一个命令 - LINUX
- amazon-s3 - 如何将大量数据从 AWS S3 自动备份到 Microsoft Azure
- python - 删除不属于城市名称字典文本文件的关键字
- spring - 君特 | main 中的自动装配组件为空