【问题标题】:How to populate table view with multiple arrays如何用多个数组填充表格视图
【发布时间】:2020-07-06 01:26:00
【问题描述】:

我正在尝试用歌曲、艺术家和专辑填充表格视图。这在我的 sql 数据库中的同一个表中,但在不同的列中。到目前为止,我只有歌曲,但没有艺术家和专辑。

var searchActive: Bool = false
var search = [Search]()

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for:  indexPath)
    
    
    if  (searchActive) {
        cell.textLabel?.text = search[indexPath.row].cleanSong
        cell.textLabel?.text = search[indexPath.row].artists
        cell.textLabel?.text = search[indexPath.row].albums
        
    
       } else {
       searchActive = true
       }
       return cell;
}

【问题讨论】:

  • 你需要不同的部分来解决这个特定的问题
  • 您必须以可以在不同部分显示的方式创建数据。您在 cell for row 方法中的现有实现将覆盖相册中的数据。

标签: ios arrays swift


【解决方案1】:

你需要一个自定义的 UITableViewCell

Here一些教程你可以试试

【讨论】:

    【解决方案2】:
    • 您的[Search] 数组是否具有艺术家和专辑价值?如果没有,请检查您如何从 sqldb 映射这些数据。

    • 填充单元格的方式是错误的 如果你打电话给这些:

    cell.textLabel?.text = search[indexPath.row].cleanSong

    cell.textLabel?.text = search[indexPath.row].artists

    cell.textLabel?.text = search[indexPath.row].albums

    假设 cleanSong、artist 和 albums 值都存在,那么您可能最终会在单元格中显示“albums”值,因为它是您在单元格中设置的最后一个值,如果您想将它们全部显示在一个单元格,您可能需要一个使用 XIB 的自定义单元格。

    【讨论】:

      【解决方案3】:

      首先,您需要自定义单元格来填充歌曲、艺术家和专辑。 您可以使用多个sections 来显示不同的内容。

      对于不同的部分:

      func numberOfSections(in tableView: UITableView) -> Int {
         return 3 // Since you have artist, album and songs content.
      }
      
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
         // Here you can return different cell as per your section and search result
      }
      

      【讨论】:

        猜你喜欢
        • 2014-01-13
        • 2018-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-19
        • 1970-01-01
        相关资源
        最近更新 更多