load more for UITableView in swift
Asked Answered
S

1

7

I have mad UITableView in swift for iOS and my Array content more then 500 elements.

How can I make page for the UITableView for Example:

once the user scroll for the latest Cell the app load more fro the 500 elements and so on.

any help?

Thanks.

Simulacrum answered 22/11, 2014 at 15:48 Comment(4)
How are you doing the table that it's not already behaving this way?Deyoung
okay but how can I add (load more) to my table, or in another way I wanna show only 10 cells from the 500 then when the user click on Load more the table load 10 cells more.Simulacrum
A good start would be including your current code here and explaining why you would want the table to only display some of the cells at once.Deyoung
thank you for replay, it's a basic table with cells from Array. and about your question because I have more then 500 element in my Array so it's slow to load them in one time and the scrolling will be slow too.Simulacrum
K
11

To answer your question made a small test! One possible solution to this problem is to make another array and add it to the data that you want to display in the table and load the data to the same that you want to load! Loading data will willDisplayCell forRowAtIndexPath

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

test project

https://drive.google.com/file/d/0B-CqajDlBoGHSE1OcjFoZWJxUTg/view?usp=sharing

Kantos answered 25/11, 2014 at 8:32 Comment(1)
thanks, work perfect for me in swift 2.0 reload more uitableviewcontrollerBellamy

© 2022 - 2024 — McMap. All rights reserved.