【问题标题】:How to display tableview sections with API Model data Arrays如何使用 API 模型数据数组显示 tableview 部分
【发布时间】:2020-03-16 04:29:48
【问题描述】:

这里我想在我的表格视图中将这些数据显示为两个部分,我有来自模型的以下数组。

在这里,我将整个数据放入一个模型中,并且两者都在顶部可见。

    var TotalbooksList : [BooksList]!
    var RecomendBooks : [BooksList]!
 var tableData: [(title: String, list: [BooksList]?)] {
           return [(title: "Recomended Books", list: RecomendBooks),
                   (title: "Total Books", list: TotalbooksList)]
       }

我编写了以下委托方法,但我没有将数据放入第二部分

func numberOfSections(in tableView: UITableView) -> Int {
    return self.tableData.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   return tableData[section].list?.count ?? 0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "RecomandBookTVCell", for: indexPath)as!RecomandBookTVCell

    if let data = self.tableData[indexPath.section].list, data.count > 0 {
        let model = data[indexPath.row]
        cell.selectionStyle = .none
        cell.booktitle.text = model.title ?? ""
        cell.bookAuthor.text = model.author ?? ""
        if model.photo != ""{
            cell.bookimage.sd_setImage(with: URL(string: Services.BaseURL + (model.photo ?? "")), placeholderImage: UIImage(named: ""))
        }
        cell.genrelbl.text = model.genre ?? ""
        cell.addlog.tag = indexPath.row
        cell.addlog.addTarget(self, action: #selector(addLog(sender:)), for: .touchUpInside)
    }
    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
   return 150
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  return 50
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    tableData[section].title
}

【问题讨论】:

  • 放置一些断点并进行调试。它会帮助你

标签: ios swift tableview sections


【解决方案1】:

您在 numberOfRowsInSection 方法中返回错误值,否则条件

您在numberOfRowsInSection 方法中使用TotalbooksList 数组作为0 部分,并在cellForRow 方法中为0 部分使用RecomendBooks 数组。更改它以使用正确的数组。

class ViewController: UIViewController {
    var totalbooksList: [BooksList]!
    var recomendBooks: [BooksList]!
    var tableData: [(title: String, list: [BooksList]?)] {
        return [(title: "Recomended Books", list: totalbooksList),
                (title: "Total Books", list: recomendBooks)]
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return self.tableData.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return tableData[section].list?.count ?? 0
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "RecomandBookTVCell", for: indexPath) as! RecomandBookTVCell

        if let data = self.tableData[indexPath.section].list, data.count > 0 {
            let model = data[indexPath.row]
            cell.selectionStyle = .none
            cell.booktitle.text = model.title ?? ""
            cell.bookAuthor.text = model.author ?? ""
            if model.photo != ""{
                cell.bookimage.sd_setImage(with: URL(string: Services.BaseURL + (model.photo ?? "")), placeholderImage: UIImage(named: ""))
            }
            cell.genrelbl.text = model.genre ?? ""
            cell.addlog.tag = indexPath.row
            cell.addlog.addTarget(self, action: #selector(addLog(sender:)), for: .touchUpInside)
        }
        return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
       return 150
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 44
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        tableData[section].title
    }
}

【讨论】:

  • 不在这里,我已经在一个单元格中将其更改为仅行的问题这里仅数据显示问题。
  • 不行,第二部分没有数据,只能合二为一。
  • @ghrixghrix 在numberOfRowsInSectioncellForRowAt 中打印TotalbooksList.countRecomendBooks.count
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-10
  • 1970-01-01
  • 2020-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-08
相关资源
最近更新 更多