Как отобразить разделы табличного представления с массивами данных модели API - PullRequest
0 голосов
/ 16 марта 2020

Здесь я хочу отобразить эти данные в виде двух разделов в моем табличном представлении. У меня есть следующие массивы из модели.

Здесь я получаю все данные в одну модель, и оба они видны сверху.

    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
}

1 Ответ

0 голосов
/ 16 марта 2020

Вы возвращаете неправильное значение в numberOfRowsInSection методе else условие

И вы используете массив 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
    }
}
...