user5034941
user5034941

Reputation:

How to make pagination in tableView?

I have implemented tableView in my app, but I have large number of data, I want to show data in pagination, means only 10 rows in 1 screen, then user can paginate and show 10 rows in next screen.

paging means I want scrolling

Is this possible using any existing component? Or I have to create custom controller? If so what will the process for it?

enter image description here

Upvotes: 2

Views: 550

Answers (3)

dopcn
dopcn

Reputation: 4218

First of all check if set your tableView's pagingEnabled = true satisfied your needs. UITableView is a subclass of UIScrollView

Upvotes: 0

Doro
Doro

Reputation: 2413

You can implement such thing using sections. Each page - one section When you are adding new section - update datasource array

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Potentially incomplete method implementation.
        // Return the number of sections.
        return _pages.count()
    }

Now, each section will represent your pages. For each section you will have your own array of data (you can hold it in a dictionary) when you need to update - call tableView.insertSections(<#sections: NSIndexSet#>, withRowAnimation: <#UITableViewRowAnimation#>)

Or, you can do update throw next method- tableView.insertRowsAtIndexPaths

Upvotes: 0

Nimit Parekh
Nimit Parekh

Reputation: 16864

var allObjectArray: NSMutableArray = []
var elements: NSMutableArray = []

var currentPage = 0
var nextpage = 0

override func viewDidLoad() {
    super.viewDidLoad()
    for var i = 0; i <= 500; i++ {
        allObjectArray.addObject(i)
    }
    elements.addObjectsFromArray(allObjectArray.subarrayWithRange(NSMakeRange(0, 20)))
}
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        println(indexPath.row)
        nextpage = elements.count - 5
        if indexPath.row == nextpage {
            currentPage++
            nextpage = elements.count - 5
            elements.addObjectsFromArray(allObjectArray.subarrayWithRange(NSMakeRange(currentPage, 20)))
                tableView.reloadData()
        }
    }

Download the sample code from here.

Upvotes: 1

Related Questions