1

My tableview has custom sections and cells. Every time the user scroll to the bottom it will call the server and load additional datas. My problem is how do I detect the last section so it could load more data? Each section contain 1 to 3 rows. My willDisplay wasn't working properly and the refreshControl.beginRefreshing() indicator didn't show too. Thanks!

let refreshControl = UIRefreshControl()

//MARK - Header Sections
func numberOfSections(in tableView: UITableView) -> Int {
    return tableData[segmentedControlIndex].count
}

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

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let cell = tableView.dequeueReusableCell(withIdentifier: "headerCell") as! HomeSectionTableViewCell

    let model = events[segmentedControlIndex][section]
    cell.setup(model: model)

    .
    .
    .

    return cell.contentView
}

//MARK - Cells
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return tableData[segmentedControlIndex][section].rows.count
}

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

    .
    .
    .

    cell.detailTitleLabel?.text = tableData[segmentedControlIndex][indexPath.section].rows[indexPath.row].detailTitleLabelString
    cell.detailSubtitleLabel?.text = tableData[segmentedControlIndex][indexPath.section].rows[indexPath.row].detailSubtitleLabelString

    return cell
}

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

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if let cell: HomeTableViewDetailCell = tableView.cellForRow(at: indexPath) as? HomeTableViewDetailCell {
        cell.detailTitleLabel?.numberOfLines = cell.detailTitleLabel?.numberOfLines == 0 ? 1 : 0            
        tableView.reloadData()
    }
}

//MARK - TableView add more data
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if indexPath.section == tableView.numberOfSections - 1 {
        self.refreshControl.beginRefreshing()
        self.page = self.page + 1
        self.getMoreData() { result in
            self.refreshControl.endRefreshing()
            print("Load More Bets Data reload: \(result == true ? "Success":"Failure")")
        }
    }
}
14079_Z
  • 401
  • 5
  • 20

0 Answers0