我试图通过按名称的第一个字母按字母顺序排列不同动漫标题的名称来向我的TableViewController添加标题。 aName是我的NSObject的属性,名为AnimeObject,我的动漫标题存在于一个名为managedAnime的数组中。然后我通过创建一个名为MyAnimeObject的NS托管对象将我的名字保存到核心数据中。我从核心数据中获取数据并对其进行排序。所以我的问题是因为我调试我注意到我的字典函数orderMyAnime只返回一个节标题或animeSectionTitles这是A.我需要知道为什么会这样,所以我可以解决它任何帮助将不胜感激。
这是我的订单MyAnime功能:
func orderMyAnime()
{
for anime in MyAnime {
//Time to create a dictionary
//print(anime.aName)
let animeKey = String(describing: anime.aName?.startIndex)
//You need to read it one by one into an array
if var animeValues = MyAnimeDict[animeKey]
{
animeValues.append(anime)
MyAnimeDict[animeKey] = animeValues
}else{
MyAnimeDict[animeKey] = [anime]
}
}
for (key, value) in MyAnimeDict{
print(String(key), terminator: ": ")
for v in value{
print(v.aName!, terminator: ", ")
}
print("", terminator: "\n")
}
//Get the section titles from the dictionary and sort them in ascending order
//print(MyAnimeDict.keys)
animeSectionTitles = [String](MyAnimeDict.keys)
print (animeSectionTitles.count, "ordermyanime")// shows that I'm only returning a single title
animeSectionTitles.sort()
}
这是我的标题功能:
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
//return animeSectionTitles[section]
if searchanimeController.isActive{
return "Search Result"
}
else{
let animeIndexTitles : [String] = ["A","B","C","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
return animeIndexTitles[section]
// return animeSectionTitles[section]
}
}
我在这里测试我是否会返回任何内容,并且我的部分数量不足:
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
if searchanimeController.isActive{
return 1
}else{
print (animeSectionTitles.count, "astcount")
return animeSectionTitles.count
}
}