这是一个简单的集合视图,具有组合布局和不同的数据源.
它显示一个类型为UICollectionViewListCell
的单元格,其contentView
具有作为子视图的文本视图.
import UIKit
class ViewController: UIViewController {
var collectionView: UICollectionView!
let textView = UITextView()
var dataSource: UICollectionViewDiffableDataSource<Section, Int>!
enum Section: CaseIterable {
case first
}
override func viewDidLoad() {
super.viewDidLoad()
configureHierarchy()
configureDataSource()
}
private func configureHierarchy() {
collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: createLayout())
view.addSubview(collectionView)
collectionView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
textView.delegate = self
}
func configureDataSource() {
let cellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, Int> { [weak self] cell, indexPath, itemIdentifier in
guard let self else { return }
cell.contentView.addSubview(textView)
textView.pinToSuperviewMargins()
}
dataSource = .init(collectionView: collectionView) { collectionView, indexPath, itemIdentifier in
collectionView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: itemIdentifier)
}
var snapshot = NSDiffableDataSourceSnapshot<Section, Int>()
snapshot.appendSections(Section.allCases)
snapshot.appendItems([1], toSection: .first)
dataSource.apply(snapshot)
}
func createLayout() -> UICollectionViewLayout {
UICollectionViewCompositionalLayout { section, layoutEnvironment in
var config = UICollectionLayoutListConfiguration(appearance: .insetGrouped)
return NSCollectionLayoutSection.list(using: config, layoutEnvironment: layoutEnvironment)
}
}
}
extension ViewController: UITextViewDelegate {
func textViewDidChange(_ textView: UITextView) {
// Do something here?
}
}
pinToSuperviewMargins方法将调用它的视图的顶部、底部、前导和尾随约束设置为其superview的,并将其translatesAutoResizingMaskIntoConstraints
属性设置为false
:
extension UIView {
func pinToSuperviewMargins(
top: CGFloat = 0,
bottom: CGFloat = 0,
leading: CGFloat = 0,
trailing: CGFloat = 0,
file: StaticString = #file,
line: UInt = #line
) {
guard let superview = self.superview else {
let localFilePath = URL(fileURLWithPath: "\(file)").lastPathComponent
print(">> \(#function) failed in file: \(localFilePath), at line: \(line): could not find \(Self.self).superView.")
return
}
self.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
self.topAnchor.constraint(equalTo: superview.topAnchor, constant: top),
self.bottomAnchor.constraint(equalTo: superview.bottomAnchor, constant: bottom),
self.leadingAnchor.constraint(equalTo: superview.leadingAnchor, constant: leading),
self.trailingAnchor.constraint(equalTo: superview.trailingAnchor, constant: trailing),
])
}
func pinToSuperviewMargins(constant c: CGFloat = 0, file: StaticString = #file, line: UInt = #line) {
self.pinToSuperviewMargins(top: c, bottom: c, leading: c, trailing: c, file: file, line: line)
}
}
我试着在textViewDidChange(_:)
中调用collectionView.setNeedsLayout()
,并将textView的isScrollEnabled
属性设置为false
,但它不起作用.
在处理表视图时,我经常通过在textViewDidChange(_:)
中调用tableView.beginUpdates(); tableView.endUpdates()
来完成单元格搜索.
我在另一个帖子中读到有人问"为什么你要把一个文本视图放在集合视图单元格中?",类似的问题在我的很多问题下都冒出来了,所以我先请你,在没有回答之前,不要问任何与这个问题无关的事情.