UITableView
— это фундаментальный UIKit-компонент для отображения прокручиваемых списков данных. Это один из самых часто используемых UI-элементов в iOS-приложениях.
let tableView = UITableView(frame: view.bounds, style: .plain)
view.addSubview(tableView)
.plain
или .grouped
)tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = items[indexPath.row].title
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let item = items[indexPath.row]
showDetail(for: item)
}
dequeueReusableCell
- сердце механизма// Более сложный пример ячейки
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
cell.configure(with: items[indexPath.row])
return cell
Auto Layout для высоты:
tableView.rowHeight = UITableView.automaticDimension
estimatedRowHeight
для оптимизацииSelf-sizing ячейки:
UICollectionView:
Diffable Data Source (iOS 13+):
SwiftUI List:
Правила быстрых таблиц:
cellForRowAt
(<1ms)Проблемные места:
UITableViewCell
class CustomCell: UITableViewCell {
func configure(with model: Item) {
// Настройка UI
}
}
UITableView
— это мощный и оптимизированный компонент для работы со списками, требующий реализации DataSource для предоставления данных и Delegate для обработки взаимодействий. Ключевые особенности — система переиспользования ячеек и плавная работа с большими объемами данных. Для новых проектов стоит рассмотреть Diffable Data Source, а для сложных layout'ов — UICollectionView.