0

enter image description hereI have a table view called challengeTable. This table contains an image, some text and a button. These values are retrieved from the RESTFUL API.

var Sec1 = [[String]]()

    public func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?{
    return sectionTitles[section]

    }
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        switch section {
        case 0 :
            return Sec1.count
        default:
            return Sec2.count

        }

    }
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let cell = challengeTable.dequeueReusableCell(withIdentifier: cellID) as! CustomisedCell
        cell.selectionStyle = .none

        switch indexPath.section {
        case 0:

            cell.CAChallengeTitle.text = Sec1[indexPath.row][0]
            cell.CAChallengeTitle.font = UIFont.boldSystemFont(ofSize: 13)
            cell.CAChallengeDescription.text = "Starting Date: \(Sec1[indexPath.row][1]) \n\n Ending Date: \(Sec1[indexPath.row][2])"
            cell.CAChallengeDescription.numberOfLines = 3
            cell.CAChallengeIMG.image = UIImage(named : "Step" )
            cell.CAChallengeButt.tag = indexPath[1]
            cell.CAChallengeButt.setTitle("Detsils >", for: .normal)
            print("Done with sectoin 0")
        default:
            cell.CAChallengeTitle.text = Sec2[indexPath.row]
            cell.CAChallengeDescription.text = "\(Sec2[indexPath.row])  "
            cell.CAChallengeButt.tag = indexPath[1]
            cell.CAChallengeButt.setTitle("I am interested >", for: .normal)
            print("Done with sectoin 1")

        }
       return cell

    }

I can reload the data of the table by calling the following:

Sec1.removeAll() 
challengeTable.reloadData()

Currently I am using the navigation controller. Thus, I can update my table simply by navigating back and forth. What I am trying to do however, is that I want to reload my table data simply when the user scrolls to the top.

Any idea how can I possibly do that? Thanks :)

Danial Kosarifa
  • 1,044
  • 4
  • 18
  • 51

2 Answers2

0

Sounds more like you are in the need of pagination/lazy loading.

is this the something what you're looking to achieve?

Reference 1

Reference 2

Naresh Reddy M
  • 1,096
  • 1
  • 10
  • 27
  • Not really what I am looking for. – Danial Kosarifa Mar 19 '18 at 06:56
  • Confirm to UIScrollViewDelegate, implement func scrollViewDidScroll(_ scrollView: UIScrollView) and you'll get the call back when user scrolled the table view, check if scrollView's content offset Y is equal to your table view's max offset Y, If both are matching, that means the table is scrolled to bottom of screen and you can reload the table here, this might do the trick – Naresh Reddy M Mar 19 '18 at 07:03
0

UIRefreshControl is what you need to satisfy your requirement.

A standard control that can initiate the refreshing of a table view’s contents.

In addition to assigning a refresh control to a table view controller’s refreshControl property, you must configure the target and action of the control itself. The control does not initiate the refresh operation directly. Instead, it sends the valueChanged event when a refresh operation should occur. You must assign an action method to this event and use it to perform whatever actions are needed.

Simply add a refreshControl to your UITableView and call reloadData on tableView's object whenever the refreshControl sends valueChanged event, i.e.

override func viewDidLoad()
{
    super.viewDidLoad()
    let refreshControl = UIRefreshControl()
    refreshControl.addTarget(self, action: #selector(reloadTable), for: .valueChanged)
    self.tableView.refreshControl = refreshControl
}

@objc func reloadTable()
{
    self.tableView.reloadData()
    self.tableView.refreshControl?.endRefreshing()
}

The UIRefreshControl itself doesn’t know when it should be hidden. To hide the control, you can simply call the endRefreshing: method.

Let me know if you still face any issues. Happy Coding..

PGDev
  • 23,751
  • 6
  • 34
  • 88