这里,我想在表格视图中将这些数据显示为两部分,该模型包含以下数组。
这里我将全部数据整合到一个模型中,并且两者都在顶部可见。
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
}
您在numberOfRowsInSection
方法中返回了错误的值,否则条件
并且您正在TotalbooksList
方法中将0
数组用于numberOfRowsInSection
部分,并将RecomendBooks
方法中的0
数组用于cellForRow
部分。更改它以使用适当的数组。
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
}
}