Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // BusListViewController.swift
- // QLead
- //
- // Created by Raed Fayad on 2018-08-26.
- // Copyright © 2018 kirkbyo. All rights reserved.
- //
- import UIKit
- import Foundation
- struct Model {
- var title: String
- var bus: String
- }
- let dataArray = [
- [Model(title: "Name1", bus: "Bus 1"), Model(title: "Name2", bus: "Bus 1"), Model(title: "Name3", bus: "Bus 1"),Model(title: "Name4", bus: "Bus 1"), Model(title: "Name5", bus: "Bus 1"), Model(title: "Name6", bus: "Bus 1"), Model(title: "Name7", bus: "Bus 1"), Model(title: "Name8", bus: "Bus 1")] ,
- [Model(title: "Name9", bus: "Bus 2"), Model(title: "Name10", bus: "Bus 2"), Model(title: "Name11", bus: "Bus 2"), Model(title: "Name12", bus: "Bus 2"), Model(title: "Name13", bus: "Bus 2")],
- [Model(title: "Name14", bus: "Bus 3"), Model(title: "Name15", bus: "Bus 3"), Model(title: "Name16", bus: "Bus 3")]]
- class ViewModelItem {
- private var item: Model
- var isSelected = false
- var title: String {
- return item.title
- }
- init(item: Model) {
- self.item = item
- }
- }
- class ViewModel: NSObject {
- var items = [ViewModelItem]()
- var didToggleSelection: ((_ hasSelection: Bool) -> ())? {
- didSet {
- didToggleSelection?(!selectedItems.isEmpty)
- }
- }
- var selectedItems: [ViewModelItem] {
- return items.filter { return $0.isSelected }
- }
- override init() {
- super.init()
- let temp = dataArray.flatMap { $0 }
- items = temp.map { ViewModelItem(item: $0) }
- }
- }
- extension ViewModel: UITableViewDataSource {
- func numberOfSectionsInTableView(tableView: UITableView) -> Int {
- return dataArray.count
- }
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return dataArray[section].count
- }
- func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
- return dataArray[section][0].bus
- }
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- if let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? BusListTableViewCell {
- cell.titleLabel.text = dataArray[indexPath.section][indexPath.row].title
- // select/deselect the cell
- if items[indexPath.row].isSelected {
- if !cell.isSelected {
- tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
- }
- } else {
- if cell.isSelected {
- tableView.deselectRow(at: indexPath, animated: false)
- }
- }
- return cell
- }
- return UITableViewCell()
- }
- }
- extension ViewModel: UITableViewDelegate {
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- // update ViewModel item
- items[indexPath.row].isSelected = true
- didToggleSelection?(!selectedItems.isEmpty)
- }
- func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
- // update ViewModel item
- items[indexPath.row].isSelected = false
- didToggleSelection?(!selectedItems.isEmpty)
- }
- func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath?{
- return indexPath
- }
- }
- class BusListViewController: UIViewController {
- var viewModel = ViewModel()
- @IBOutlet weak var tableView: UITableView!
- @IBOutlet weak var nextButton: UIButton!
- @IBAction func buttonPressed(_ sender: Any) {
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- tableView?.estimatedRowHeight = 100
- tableView?.rowHeight = UITableViewAutomaticDimension
- tableView?.allowsMultipleSelection = true
- tableView?.dataSource = viewModel
- tableView?.delegate = viewModel
- tableView?.separatorStyle = .none
- viewModel.didToggleSelection = { [weak self] hasSelection in
- self?.nextButton.isEnabled = hasSelection
- }
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- }
Add Comment
Please, Sign In to add comment