Been trying to solve this for over 10 days now and still haven't had any luck fixing the situation.
I am trying to add sections to the table view in alphabetical order from A-Z based on the names of "ExerciseName" column. Also i have a "BodyPartName", "CategoryName"? column of data for each "ExerciseName".
I want to populate sections from A-Z and have the ExerciseName sorted into each section with BodyPartName & CategoryName for other labels in same row.
I was successful in implementing ExerciseName under sections A-Z, but unable to add BodyPartName & CategoryName into other labels in same row. Please Help!!
Result Expected
A
Abs "(Core)"
Arm Press "(Arms)" "(Dumbbell)"
B
Bicep Curl "(Arms)" "(Barbell)"
Back Extension "(Back)" "(Machine)"
Unable to populate "BodyPartName" label & "CategoryName"label for same row.
This is my Realm DataModel:
class Exercises: Object
{
@objc dynamic var ExerciseID = 0
@objc dynamic var ExerciseName = ""
@objc dynamic var BodyPartName = ""
@objc dynamic var CategoryName: String? = ""
//Adding Parent Table BodyPart & Category.
var parentBodyParts = LinkingObjects(fromType: BodyParts.self, property: "exercises")
var parentCategory = LinkingObjects(fromType: Category.self, property: "category")
}
My Code:
let realm = try! Realm()
var exerciseArray: Results<Exercises>!
override func viewDidLoad()
{
super.viewDidLoad()
tableView.register(UINib(nibName: "customCell", bundle: nil), forCellReuseIdentifier: cellID)
sectionFunction()
}
var sectionDictionary = [String:[String]]()
var sectionTitles = [String]()
func sectionFunction()
{
exerciseArray = realm.objects(Exercises.self).sorted(byKeyPath: "ExerciseName")
for section in exerciseArray
{
let sectionKey = String(section.ExerciseName.prefix(1))
if var sectionValues = sectionDictionary[sectionKey]
{
sectionValues.append(section.ExerciseName) // Adding ExerciseNames to the Keys
sectionDictionary[sectionKey] = sectionValues
}
else
{
sectionDictionary[sectionKey] = [section.ExerciseName]
}
}
sectionTitles = [String](sectionDictionary.keys)
sectionTitles = sectionTitles.sorted(by: {$0 < $1}) // Alphabetical order for Keys:Values
}
override func numberOfSections(in tableView: UITableView) -> Int
{
return sectionTitles.count
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?
{
return sectionTitles[section]
}
override func sectionIndexTitles(for tableView: UITableView) -> [String]?
{
return sectionTitles
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
let sectionKey = sectionTitles[section]
if let sectionValues = sectionDictionary[sectionKey]
{
return sectionValues.count
}
return 0
// return exercises.filter("ExerciseName == %@", sectionNames[section]).count
}
// Register custom cell for Table View
let cellID = "customCell"
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! customCell
let sectionKey = sectionTitles[indexPath.section]
if let sectionValues = sectionDictionary[sectionKey]
{
exerciseArray = realm.objects(Exercises.self).sorted(byKeyPath: "ExerciseName")
cell.exerciseName.text = sectionValues[indexPath.row]
cell.bodyPartName.text = exerciseArray.filter("ExerciseName == %@", sectionValues[indexPath.row]) [indexPath.row].BodyPartName
cell.categoryName.text = exerciseArray.filter("ExerciseName == %@", sectionValues[indexPath.row]) [indexPath.row].CategoryName ?? ""
cell.backgroundColor = .clear
}
return cell
}
Custom Cell:
class customCell: UITableViewCell {
@IBOutlet weak var exerciseName: UILabel!
@IBOutlet weak var bodyPartName: UILabel!
@IBOutlet weak var categoryName: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}