首页 > 解决方案 > 使用自定义部分和单元格 Swift 滚动到底部 TableView

问题描述

tableview有自定义sectionscells. 每次用户滚动到底部时,它都会调用服务器并加载额外的数据。我的问题是如何检测最后一个section以便加载更多数据?每个section包含 1 到 3 rows。我willDisplay的工作不正常,refreshControl.beginRefreshing()指标也没有显示。谢谢!

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")")
        }
    }
}

标签: swifttableview

解决方案


推荐阅读