swift - 列出专辑标题时如何使用右侧的表格 View "index"?

标签 swift indexing tableview mpmediaquery

我正在使用 Xcode 10.3,并编写了一个音乐程序,该程序已经发展了很长时间并且几乎完全可以工作。当我使用 query = MPMediaQuery.songs() 时,我可以完美地使用屏幕右侧的索引。但是,当我使用 query = MPMediaQuery.albums() 时,我只能获取以 A 开头的专辑标题。仅此而已。然而,共有 464 张专辑(只有 27 张以字母 A 开头)。

这可能看起来是一个“重复问题”。大部分代码是 weyney 大约 4 年前编写的。那时这对他有用。从那时起, swift 就发生了变化。当我使用 numberOfSectionsInTableView (带有专辑,就像他一样)时,我在“A”下获得了前 27 首歌曲。当我使用 numberOfSections (带有专辑)时,它会崩溃(但不适用于歌曲)。

您会注意到我从未获得任何索引或节数的打印输出。

let qryAlbums = MPMediaQuery.albums()
query.groupingType = MPMediaGrouping.album


// Display the index of the table on the right-hand side of the screen
func sectionIndexTitles(for tableView: UITableView) -> [String]? {

    let sectionIndexTitles = query.itemSections!.map { $0.title }

    print("sectionIndexTitles = ", sectionIndexTitles)
    return sectionIndexTitles
}

// Set the alignment of the section header in the table
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

    let header = view as! UITableViewHeaderFooterView
    header.textLabel!.textAlignment = NSTextAlignment.center
}

// Get the index of the section
func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {

    print("index = ", index)
    return index
}

// Display a header title in each section
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

    print("query.itemSections![section].title = ", query.itemSections![section].title)
    return (query.itemSections![section].title)
}

// Get the number of sections
func numberOfSectionsInTableView(in tableView: UITableView) -> Int {

    print("query.itemSections?.count = ", query.itemSections?.count ?? 0)
    return (query.itemSections?.count)!
}

// Get the number of rows per Section - YES SECTIONS EXIST WITHIN QUERIES
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    print("query![section].range.length = ", query.collectionSections![section].range.length)
    return query.collectionSections![section].range.length
}

// Set the cell in the table
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    // I am using the custom class that I created called: myCell
    let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! myTableViewCell

    let currLoc = query.collectionSections![indexPath.section].range.location
    let rowItem = query.collections![indexPath.row + currLoc]

    // Main text is Album name
    cell.title.text = rowItem.items[0].albumTitle
}

输出:

query.itemSections![section].title = A

查询![节].range.length = 27

query.itemSections![section].title = A

查询![节].range.length = 27

query.itemSections![section].title = A

查询![节].range.length = 27

sectionIndexTitles = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", “L”、“M”、“N”、“O”、“P”、“Q”、“R”、“S”、“T”、“U”、“V”、“W”、“X” ”、“Y”、“Z”、“#”、“?”]

query.itemSections![section].title = A

我几乎花了一天的大部分时间来寻找解决方案。我真的很感激任何帮助。提前致谢!

最佳答案

我终于找到了。我不得不更换一个?与一个! (并减少计数)在 func numberOfSections 中。

// Get the number of sections
func numberOfSections(in tableView: UITableView) -> Int {

    let numOfSections = query.itemSections!.count - 1
    return numOfSections
}

希望这可以帮助其他可能遇到困难的人。

关于swift - 列出专辑标题时如何使用右侧的表格 View "index"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59595582/

相关文章:

ios - 滚动 UITableView 方法

用于大型数据库表的 JavaFx TableView CRUD

java - JavaFX 中的复选框单元格工厂 + Tableview

ios - 如何从cloudkit下载多条记录

swift:如何删除特定字符?

mongodb - 使用 Solr 索引嵌入式 mongoDB 文档(在数组中)

indexing - 范围查询如何在 LSM(日志结构合并树)上工作?

ios - CallKit 不会阻止数组中的数字

ios - 带有 UITableViewController 的 SearchBar 包含错误数量的单元格

python - 如何通过循环向后访问字符串的一部分