load more for UITableView in swift

半世苍凉 提交于 2019-11-30 07:31:42

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/27079253/load-more-for-uitableview-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!