过去尝试解决这一问题已有十多天了,但仍然没有运气来解决这种情况。
我正在尝试根据“ ExerciseName”列的名称,按字母顺序从A-Z向表视图添加节。我也有一个“ BodyPartName”,“ CategoryName”?每个“ ExerciseName”的数据列。
我想填充A-Z中的部分,并在同一部分中将ExerciseName排序为BodyPartName和CategoryName,以用于同一行中的其他标签。
我成功地在A-Z部分实现了ExerciseName,但是无法将BodyPartName和CategoryName添加到同一行的其他标签中。请帮忙!
预期结果
A
Abs“(核心)”
手臂按“(武器)”“(哑铃)”
B
二头肌卷曲“(手臂)”“(杠铃)”
后退扩展名“(后退)”“(机器)”
无法为同一行填充“ BodyPartName”标签和“ CategoryName”标签。
这是我的领域数据模型:
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")
}
我的代码:
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
}
自定义单元格:
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)
}
}