Konuyla alakalı basit bir örnek hazırladım.
//
// ViewController.swift
// tableViewCheckMark
//
// Created by Yasin TURKOGLU on 22.04.2018.
// Copyright © 2018 Yasin TURKOGLU. All rights reserved.
//
import UIKit
typealias Person = (name: String, age: UInt)
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UINavigationBarDelegate {
let aTableView:UITableView = {
let tableView = UITableView()
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.tableFooterView = UIView()
tableView.separatorInset = .zero
tableView.separatorStyle = .singleLine
tableView.allowsSelection = true
tableView.allowsMultipleSelection = false
return tableView
}()
let navigationBar:UINavigationBar = {
let bar = UINavigationBar()
bar.barTintColor = .orange
bar.translatesAutoresizingMaskIntoConstraints = false
return bar
}()
let navigationBarTitle = UINavigationItem()
let persons:[Person] = [
Person(name: "Ahmet", age: 11),
Person(name: "Mehmet", age: 14),
Person(name: "Ayşe", age: 17),
Person(name: "Fatma", age: 10),
Person(name: "Hasan", age: 9),
Person(name: "Hüseyin", age: 18),
Person(name: "Zeynep", age: 16),
Person(name: "Dilek", age: 13)
]
override func viewDidLoad() {
super.viewDidLoad()
self.navigationBar.delegate = self
self.navigationBar.items = [self.navigationBarTitle]
self.view.addSubview(self.navigationBar)
self.aTableView.delegate = self
self.aTableView.dataSource = self
self.view.addSubview(self.aTableView)
if #available(iOS 11.0, *) {
NSLayoutConstraint.activate([
self.navigationBar.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
self.navigationBar.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
self.navigationBar.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
self.aTableView.topAnchor.constraint(equalTo: self.navigationBar.bottomAnchor),
self.aTableView.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor),
self.aTableView.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor),
self.aTableView.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor)
])
} else {
NSLayoutConstraint.activate([
self.navigationBar.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
self.navigationBar.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
self.navigationBar.topAnchor.constraint(equalTo: self.topLayoutGuide.topAnchor),
self.aTableView.topAnchor.constraint(equalTo: self.navigationBar.bottomAnchor),
self.aTableView.bottomAnchor.constraint(equalTo: self.bottomLayoutGuide.bottomAnchor),
self.aTableView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
self.aTableView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor)
])
}
self.view.layoutIfNeeded()
calculateAgesTotal()
}
func position(for bar: UIBarPositioning) -> UIBarPosition {
return .topAttached
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.persons.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50.0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell = {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") else {
return UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "cell")
}
return cell
}()
let person = self.persons[indexPath.row]
cell.textLabel?.text = "\(person.name)"
cell.detailTextLabel?.text = "Age: \(person.age)"
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) {
if cell.accessoryType == .checkmark{
cell.accessoryType = .none
} else{
cell.accessoryType = .checkmark
}
}
calculateAgesTotal()
tableView.deselectRow(at: indexPath, animated: true)
}
func calculateAgesTotal() {
var selectedAgeTotals:UInt = 0
for i in 0..<self.persons.count {
let indexPath = IndexPath(row: i, section: 0)
if let cellAtIndexPath = self.aTableView.cellForRow(at: indexPath) {
if cellAtIndexPath.accessoryType == .checkmark{
if let detailTextLabel = cellAtIndexPath.detailTextLabel {
if let ageText = detailTextLabel.text {
let isolatedAgeString = ageText.replacingOccurrences(of: "Age: ", with: "")
if let age = UInt(isolatedAgeString) {
selectedAgeTotals += age
}
}
}
}
}
}
if selectedAgeTotals > 0 {
self.navigationBarTitle.title = "Selected ages total \(selectedAgeTotals)"
} else {
self.navigationBarTitle.title = "No person selected"
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}