Last active
July 10, 2021 15:41
-
-
Save Bilguun132/cda7264937581be8b8367212ef173af9 to your computer and use it in GitHub Desktop.
TableView-Tutorial-Alphabetical Index
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// User.swift | |
// TableViewDemo | |
// | |
// Created by Bilguun Batbold on 22/2/19. | |
// Copyright © 2019 ISEM. All rights reserved. | |
// | |
import Foundation | |
public struct User { | |
let name: String | |
let gender: String | |
let email: String | |
} | |
public class UserManager { | |
//set array of users (in reality this is usually from a network call | |
public var users: [User] = [User(name: "Albert", gender: "Male", email: "albert@gmail.com"), User(name: "Bob", gender: "Male", email: "bob@gmail.com"), User(name: "Celine", gender: "Female", email: "celine@gmail.com"), User(name: "Derrick", gender: "Male", email: "derrick@gmail.com"), User(name: "Aldwin", gender: "Male", email: "aldwin@gmail.com")] | |
private var userSection = [String]() | |
private var userDictionary = [String:[User]]() | |
//invoke sort user function when initialized | |
init() { | |
sortUser() | |
} | |
//this functions goes through each user and sets the first letter as a key. It then populates the list of users with the same key | |
// this way we will end up with dictionary of users under their respecive alphabetical order | |
private func sortUser() { | |
for user in users { | |
let key = "\(user.name[user.name.startIndex])".uppercased() | |
if var userValue = self.userDictionary[key] { | |
userValue.append(user) | |
} else { | |
self.userDictionary[key] = [user] | |
} | |
self.userSection = [String](self.userDictionary.keys).sorted() | |
} | |
} | |
public var userSections: [String] { | |
return userSection | |
} | |
public var userDictionaries: [String:[User]] { | |
return userDictionary | |
} | |
//return total count | |
public var userCount: Int { | |
return users.count | |
} | |
public func addUser(user: User) { | |
users.append(user) | |
} | |
//get user | |
public func getUser(at index:Int) -> User { | |
return users[index] | |
} | |
public func deleteUser(at index: Int) { | |
users.remove(at: index) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UserDataSourceProvider.swift | |
// TableViewDemo | |
// | |
// Created by Bilguun Batbold on 22/2/19. | |
// Copyright © 2019 ISEM. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
public class UserDataSourceProvider: NSObject, UITableViewDelegate, UITableViewDataSource { | |
//private var to hold the user manager | |
private let userManager: UserManager | |
//initialize the user manager | |
init(userManager: UserManager) { | |
self.userManager = userManager | |
} | |
//tableview delegates declared here | |
//set the height of header | |
public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { | |
return 30 | |
} | |
//set the number of sections | |
public func numberOfSections(in tableView: UITableView) -> Int { | |
return userManager.userSections.count | |
} | |
//set the section index titles | |
public func sectionIndexTitles(for tableView: UITableView) -> [String]? { | |
return userManager.userSections | |
} | |
//set the section header titles | |
public func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { | |
return userManager.userSections[section].uppercased() | |
} | |
//set the header view | |
public func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { | |
view.tintColor = #colorLiteral(red: 0.3333333333, green: 0.6392156863, blue: 0.9333333333, alpha: 1) | |
let header = view as! UITableViewHeaderFooterView | |
header.textLabel?.font = UIFont.systemFont(ofSize: 20, weight: .light) | |
header.textLabel?.textColor = UIColor.white | |
} | |
//sets the nubmer of rows per section | |
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
let userKey = userManager.userSections[section] | |
if let users = userManager.userDictionaries[userKey] { | |
return users.count | |
} | |
return 0 | |
} | |
//populate the user from the dictionary instead of the array now | |
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! UserTableViewCell | |
let userKey = userManager.userSections[indexPath.section] | |
if let users = userManager.userDictionaries[userKey.uppercased()] { | |
cell.configure(user: users[indexPath.row]) | |
} | |
return cell | |
} | |
public func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { | |
let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in | |
self.userManager.deleteUser(at: indexPath.row) | |
tableView.deleteRows(at: [indexPath], with: .fade) | |
} | |
return [delete] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does this update the section titles if a row is deleted?