使用UITableView加载更多选项

Gun*_*tel 0 pagination uitableview ios swift2

尝试使用分页在页面中加载我的数据,我已经看过很多例子,但都是在Objective-C中,有些问题没有答案.

我的代码:

class testTableViewController: UITableViewController {

    //MARK: - Properties -

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

    var currentPage = 0 //number of current page
    var nextpage = 0

    var selectedRow = Int()

    //MARK: - View Life Cycle -

    override func viewDidLoad() {
        super.viewDidLoad()

        for var i = 1; i < 500; i++ {
            allObjectArray.addObject(i)
        }
        elements.addObjectsFromArray(allObjectArray.subarrayWithRange(NSMakeRange(0, 30)))
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    // MARK: - Table view data source -

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return elements.count + 1
    }
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        selectedRow = (tableView.indexPathForSelectedRow?.row)!
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var customCell = tableView.dequeueReusableCellWithIdentifier("cell")
        customCell = UITableViewCell(style: .Default, reuseIdentifier: "cell")

        customCell!.textLabel!.text = "cell - \(allObjectArray[indexPath.row])"

        if indexPath.row == elements.count  {
            customCell?.textLabel?.textColor = UIColor.blueColor()
            customCell?.textLabel?.text = "Load more..."
        }
        return customCell!
    }
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        nextpage = elements.count

        if indexPath.row == nextpage {
            if indexPath.row == selectedRow {
                currentPage++
                nextpage = elements.count - 5
                elements.addObjectsFromArray(allObjectArray.subarrayWithRange(NSMakeRange(currentPage, 30)))
                tableView.reloadData()
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我想要这种输出:

在此输入图像描述

试图获取所选索引,但它将返回nil.

小智 5

在Interface Builder中创建Outlets用于tableview,并使两个动态原型单元格为它们提供标识符,使用一个按钮创建一个单元格(加载更多单元格按钮)然后使用该按钮创建操作,该按钮将包含加载更多单元格的逻辑!

现在看下面的snipet作为参考......

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

@IBOutlet weak var tblDemo: UITableView!
var arForCells:NSMutableArray = NSMutableArray()
let simpleCellID = "SimpleCell"
let loadMoreCell = "LoadMoreCell"

override func viewDidLoad() {
    super.viewDidLoad()
    tblDemo.delegate = self
    tblDemo.dataSource = self
    arForCells  = NSMutableArray(objects: "1", "2", "3", "4", "5", "6", "7", "8", "9", "10")
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return arForCells.count + 1
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if (indexPath.row == arForCells.count){
        let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(loadMoreCell, forIndexPath: indexPath)
        return cell
    }else {
        let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier(simpleCellID, forIndexPath: indexPath)

        let lblCounter = cell.viewWithTag(111) as! UILabel
        lblCounter.text = arForCells.objectAtIndex(indexPath.row) as! String
        return cell
    }
}

@IBAction func loadMoreCells(sender: AnyObject) {
    let newAr:NSArray = NSArray(objects:  "1", "2", "3", "4", "5", "6", "7", "8", "9", "10")

    arForCells.addObjectsFromArray(newAr as [AnyObject])
    tblDemo.reloadData()
}}
Run Code Online (Sandbox Code Playgroud)

正如我已经检查过,它应该给你想要的结果.

您也可以在TableViewFooter中执行相同的操作.