I'm new to this, so this question might be stupid.
I have made some indexed table view with user names. Now I added a label with user surnames and I want to make my indexed table view sorted by user's surnames using data model and I just really have no idea how to do that.
It may be helpful, so here you can watch how my app is working right now.
The first, there are FriendsSearchViewController
with all users template.
import UIKit
final class FriendsSearchViewController: UITableViewController {
var friends = [
"Polina",
"Ivan",
"Pavel",
"Maria",
"Nick"
]
var userFriends: [String] = []
var friendSectionTitles = [String]()
var friendsDictionary = [String: [String]]()
// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(
nibName: "FriendCell",
bundle: nil),
forCellReuseIdentifier: "friendCell")
for friend in friends {
let friendKey = String(friend.prefix(1))
if var friendValues = friendsDictionary[friendKey] {
friendValues.append(friend)
friendsDictionary[friendKey] = friendValues
} else {
friendsDictionary[friendKey] = [friend]
}
}
friendSectionTitles = [String](friendsDictionary.keys)
friendSectionTitles = friendSectionTitles.sorted(by: { $0 < $1 })
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
friendSectionTitles.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let friendKey = friendSectionTitles[section]
if let friendValues = friendsDictionary[friendKey] {
return friendValues.count
}
return 0
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
friendSectionTitles[section]
}
override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
friendSectionTitles
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard
let cell = tableView.dequeueReusableCell(withIdentifier: "friendCell", for: indexPath) as? FriendCell
else { return UITableViewCell() }
var currentFriend = friends[indexPath.row]
let friendKey = friendSectionTitles[indexPath.section]
if let friendValues = friendsDictionary[friendKey] {
currentFriend = friendValues[indexPath.row]
}
cell.configure(
photo: UIImage(named: "\(indexPath.row)") ?? UIImage(),
name: currentFriend,
surname: "")
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
defer {
tableView.deselectRow(at: indexPath, animated: true)
}
let friendKey = friendSectionTitles[indexPath.section]
var currentFriend = ""
if let friendValues = friendsDictionary[friendKey] {
currentFriend = friendValues[indexPath.row]
}
if userFriends.firstIndex(of: currentFriend) == nil {
userFriends.append(currentFriend)
}
self.performSegue(withIdentifier: "addFriend", sender: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "addFriend",
let myFriendsViewController = segue.destination as? MyFriendsViewController {
myFriendsViewController.friends = userFriends
}
}
}
And there are MyFriendsViewController
with users that have been added to friends list:
import UIKit
final class MyFriendsViewController: UITableViewController {
var friends = [String]() {
didSet {
//
}
}
@IBAction func addFriend(segue: UIStoryboardSegue) {
guard segue.identifier == "addFriend",
let allFriendsViewController = segue.source as? FriendsSearchViewController
else { return }
friends = allFriendsViewController.userFriends
tableView.reloadData()
}
// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(
nibName: "FriendCell",
bundle: nil),
forCellReuseIdentifier: "friendCell")
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
friends.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard
let cell = tableView.dequeueReusableCell(withIdentifier: "friendCell", for: indexPath) as? FriendCell
else { return UITableViewCell() }
let currentFriend = friends[indexPath.row]
cell.configure(
photo: UIImage(named: "\(indexPath.row)") ?? UIImage(),
name: currentFriend,
surname: "")
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
defer { tableView.deselectRow(
at: indexPath,
animated: true)}
performSegue(
withIdentifier: "showProfile",
sender: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "addFriend",
let allFriendsViewController = segue.destination as? FriendsSearchViewController {
allFriendsViewController.userFriends = friends
}
}
}
Also there are UserModel
that probably looks not correctly:
import UIKit
struct UserModel {
let userName: String
let userSurname: String
let userPhoto: UIImage
let userAge: String
}
And FriendCell
with cell configuration:
import UIKit
class FriendCell: UITableViewCell {
@IBOutlet var friendPhoto: AvatarImage!
@IBOutlet var friendName: UILabel!
@IBOutlet var friendSurname: UILabel!
func configure(
photo: UIImage,
name: String,
surname: String) {
self.friendPhoto.image = photo
self.friendName.text = name
self.friendSurname.text = surname
}
}
I'm just cannot imagine what should I do. How do I should made it? Please, can you give me some ideas with code examples? Thank you!