-3

Here I want to display these data into two sections in my table view, I have the following arrays from the model.

Here I getting entire data into a single model and both are visible at the top.

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

I write following Delegate Methods but I don't get data into a second section

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
}
ghrix ghrix
  • 29
  • 3
  • 7

1 Answers1

0

You are returning wrong value in numberOfRowsInSection method else condition

And you are using TotalbooksList array for section 0 in numberOfRowsInSection method and using RecomendBooks array for section 0 in cellForRow method. Change it to use proper array.

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
    }
}
iOSDev
  • 199
  • 11