Теперь я хочу показать таблицу с N строк и N столбцов с направленной прокруткой.
Ограничения
С помощью композиционной прокрутки макет, нам нужно создать табличную структуру CollectionView. Ниже приведены свойства.
- считайте, что максимальная ширина ячейки равна 130, а высота может увеличиваться в зависимости от содержимого.
- высота ячейки должна быть одинаковой во всех строках, а ширина ячейки должна быть одинаковой во всех столбцах.
- ячейка может состоять только из меток, и нет никаких ограничений на отображение количество строк. Таким образом, оно может расширяться в зависимости от содержимого.
- в представлении может быть n таблиц.
В моем коде применяется вывод, имеющий одинаковую высоту для всей группы (строки). **Но мне также нужна одинаковая ширина для всего столбца в разделе для всех столбцов. Это не применяется
**
Мой код представления контроллера:
Код: Выделить всё
class ViewController: UIViewController, UICollectionViewDataSource {
var modelTableData: ModelTabledata! = nil
var modelData : [TableData]! = nil
var flattenedData : [FlattenData]! = nil
private var collectionView : UICollectionView! = nil
private func createLayout() -> UICollectionView {
let layout = UICollectionViewCompositionalLayout { (sectionIndex, _) -> NSCollectionLayoutSection in
let item = NSCollectionLayoutItem(layoutSize: .init(widthDimension: .estimated(100), heightDimension: .uniformAcrossSiblings(estimate: 100)))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: .init(widthDimension: .estimated(100), heightDimension: .estimated(100)), subitems: Array(repeating: item, count: (self.modelData[sectionIndex].data.headers.count)))
// Define the container group where groups will be laid out vertically
let containerGroup = NSCollectionLayoutGroup.vertical(layoutSize: .init(widthDimension: .estimated(100), heightDimension: .estimated(100)), subitems: Array(repeating: group, count: (self.modelData[sectionIndex].data.rows.count + 1)))
// Create a section with the container group
let section = NSCollectionLayoutSection(group: containerGroup)
// Define the size of the section header
let headerSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .estimated(44))
let sectionHeader = NSCollectionLayoutBoundarySupplementaryItem(layoutSize: headerSize, elementKind: UICollectionView.elementKindSectionHeader, alignment: .top)
section.boundarySupplementaryItems = [sectionHeader]
// Set orthogonal scrolling behavior for the section
section.orthogonalScrollingBehavior = .continuous
return section
}
return UICollectionView(frame: .zero, collectionViewLayout: layout)
}
override func viewDidLoad() {
super.viewDidLoad()
modelTableData = ModelTabledata()
modelData = modelTableData.jsonContents
flattenedData = modelTableData.flattenData()
self.view.backgroundColor = .blue
collectionView = createLayout()
collectionView.dataSource = self
collectionView.register(CollectionViewCell.self, forCellWithReuseIdentifier: CollectionViewCell.cellId)
collectionView.register(TableViewTitle.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: TableViewTitle.sectionHeaderId)
setupCollectionView()
}
func setupCollectionView() {
self.view.addSubview(collectionView)
collectionView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
collectionView.topAnchor.constraint(equalTo: view.topAnchor),
collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return flattenedData.count
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return flattenedData[section].data.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CollectionViewCell.cellId, for: indexPath) as! CollectionViewCell
let sectionData = flattenedData[indexPath.section]
let rowData = sectionData.data[indexPath.item]
cell.configure(with: rowData)
return cell
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
// Configure section header
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: TableViewTitle.sectionHeaderId, for: indexPath) as! TableViewTitle
headerView.titleLabel.text = "Section \(indexPath.section)"
return headerView
}
}
Код: Выделить всё
struct TableData: Hashable, Codable {
var type: String
var data: Table
var title: String
}
struct Table: Codable, Hashable {
var rows: [[String]]
var headers: [String]
}
Код: Выделить всё
struct FlattenData {
var type: String
var data: [String]
var title: String
}
@Observable
class ModelTabledata {
var jsonContents: [TableData] = load("data.json")
func flattenData() -> [FlattenData] {
var customizedData: [FlattenData] = [ ]
jsonContents.prefix(1).forEach{ table in
var cellsData : [String] = table.data.rows.flatMap{ $0 }
cellsData.insert(contentsOf: table.data.headers, at: 0)
customizedData.append(FlattenData(type: table.type, data: cellsData, title: table.title))
}
return customizedData
}
}
func load(_ filename: String) -> T {
let data: Data
guard let file = Bundle.main.url(forResource: filename, withExtension: nil)
else {
fatalError("Couldn't find \(filename) in main bundle.")
}
do {
data = try Data(contentsOf: file)
// print(data)
} catch {
fatalError("Couldn't load \(filename) from main bundle:\n\(error)")
}
do {
let decoder = JSONDecoder()
return try decoder.decode(T.self, from: data)
} catch {
fatalError("Couldn't parse \(filename) as \(T.self):\n\(error)")
}
}
Источник: https://stackoverflow.com/questions/781 ... a-table-in