使用UITableView加载更多选项

时间:2015-10-05 10:56:55

标签: ios uitableview pagination 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()
            }
        }
    }
}

我想要这种输出:

enter image description here

尝试获取所选索引,但它将返回nil。

5 个答案:

答案 0 :(得分:5)

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

现在请看下面的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()
}}

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

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

答案 1 :(得分:1)

首先,你不必使用它:

selectedRow = (tableView.indexPathForSelectedRow?.row)!
在didSelectRowAtIndexPath中。你可以简单地使用

selectedRow = indexPath.row

接下来,如果您想让单元格点击它,那么您对willDisplayCellAtIndexPath的逻辑似乎是多余的。您只需将以下内容放入enter code here

即可
nextpage = elements.count
if indexPath.row == nextpage {
     currentPage++
     nextpage = elements.count - 5
     elements.addObjectsFromArray(allObjectArray.subarrayWithRange(NSMakeRange(currentPage, 30)))
     tableView.reloadData()
}

另外,我不确定你为什么需要nextpage = elements.count - 5,但我认为你背后有理由。

答案 2 :(得分:1)

完成了我的自我

import UIKit

class LoadMoreTableVC: UITableViewController {

    //MARK: - Properties -

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

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

    var totalElements = 500 //total elements
    var elementAtOnePage = 30 //at one page

    //MARK: - View Life Cycle -

    override func viewDidLoad() {
        super.viewDidLoad()

        for i in 0 ..< totalElements {
            allObjectArray.addObject(i+1)
        }
        elements.addObjectsFromArray(allObjectArray.subarrayWithRange(NSMakeRange(0, elementAtOnePage)))
    }
    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 {

        let count = elements.count + 1

        if count < allObjectArray.count
        {
            return count
        }
        else
        {
            return allObjectArray.count
        }
    }
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        if indexPath.row == elements.count {
            loadDataDelayed()
        }
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
        cell.textLabel!.text = "\(allObjectArray[indexPath.row])"
        if indexPath.row == elements.count  {
            cell.textLabel?.text = "Load more..."
        }
        return cell
    }
    func loadDataDelayed(){
        currentPage += 1
        elements.addObjectsFromArray(allObjectArray.subarrayWithRange(NSMakeRange(currentPage, elementAtOnePage)))
        tableView.reloadData()
    }
}

答案 3 :(得分:0)

我已经做到了这一点很快。

   func scrollViewDidEndDecelerating(scrollView: UIScrollView) {

      if loading == true {

        var endScrolling:CGFloat = scrollView.contentOffset.y +   scrollView.frame.size.height

        if(endScrolling >= scrollView.contentSize.height){

            NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "loadDataDelayed", userInfo: nil, repeats: false)


        }

    }

}

请尝试此代码。

并分享您的回复。

快乐编码:)

答案 4 :(得分:0)

要设置“加载更多..”文本,请在与tableViewCell大小相同的视图中添加带有此文本的标签。然后在tableView页脚中添加它。在页脚视图中添加(自定义/透明)按钮,以便在触摸它时将为主数组加载更多数据,然后重新加载tableView。

希望这有帮助!