Skip to content

Instantly share code, notes, and snippets.

View TungVuDuc2805's full-sized avatar
💭
Learning

Tung Vu Duc TungVuDuc2805

💭
Learning
View GitHub Profile
init(){
let layout = UICollectionViewCompositionalLayout { (section, env) -> NSCollectionLayoutSection? in
// 1
let item = NSCollectionLayoutItem(layoutSize: .init(widthDimension: .fractionalWidth(1.0), heightDimension: .absolute(200)))
// 2
let group = NSCollectionLayoutGroup.horizontal(layoutSize: NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .absolute(200)), subitems: [item])
// 3
let section = NSCollectionLayoutSection(group: group)
return section
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
//cell.imageView.image = ...
//cell.titleLabel.text = ...
} else if indexPath.row == 1 {
//configure cell type 2
}
...
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
// event when tap on cell at indexPath = 0
} else if indexPath.row == 1 {
// event when tap on cell at indexPath = 0
}
...
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
//cell.model = models[indexPath.row]
} else if indexPath.row == 1 {
//cell.model = models[indexPath.row]
}
...
}
class ProfileViewModel: NSObject, UITableViewDataSource {
var items: [ProfileSectionControllerProtocol] = []
func numberOfSections(in tableView: UITableView) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items[section].rowCount
}
protocol ProfileLoader {
func load() -> [ProfileSectionControllerProtocol]
}
class RemoteProfileLoader: ProfileLoader {
func load() -> [ProfileSectionControllerProtocol] {
var items = [ProfileSectionControllerProtocol]()
guard let data = dataFromFile("ServerData"), let profile = Profile(data: data) else {
return []
class ProfileViewModelNamePictureItem {
var name: String
var pictureUrl: String
init(name: String, pictureUrl: String) {
self.name = name
self.pictureUrl = pictureUrl
}
}
protocol ProfileSectionControllerProtocol {
var title: String {get}
var cells: [ProfileCellProtocol] {get set}
var rowCount: Int {get}
init(title: String, cells: [ProfileCellProtocol])
}
extension ProfileSectionControllerProtocol {
var rowCount: Int {
return cells.count
import UIKit
class ProfileNamePictureCellController: ProfileCellProtocol {
private var cell: NamePictureCell?
private let dataModel: ProfileViewModelNamePictureItem
init(dataModel: ProfileViewModelNamePictureItem) {
self.dataModel = dataModel
}
protocol ProfileCellProtocol {
func view(_ tableView: UITableView) -> UITableViewCell
}