用UITableView加载更多的选项

试图使用分页加载我的数据在页面中,我已经看到了很多例子,但都在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() } } } } 

我想要这样的输出:

在这里输入图像说明

试图获取选定的索引,但它将返回零。

在Interface Builder中为tableview创build出口,并使两个dynamic的原型单元格给他们的标识符使一个单元格与一个button(您的加载更多的单元格button)然后创build操作与该button,将包含加载更多单元格的逻辑!

现在看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中做同样的事情。

首先,你不必使用这个:

 selectedRow = (tableView.indexPathForSelectedRow?.row)! 

在didSelectRowAtIndexPath中。 你可以使用简单

 selectedRow = indexPath.row 

接下来,如果你想让单元格点击它,你的逻辑willDisplayCellAtIndexPath似乎是多余的。 你可以在enter code here以下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但我认为你有这个背后的理由。

我已经做到了这一点。

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

请试试这个代码。

并分享你的回应。

快乐编码:)

要设置“加载更多..”文本,请在具有与您的tableViewCell大小相同的视图中添加带有此文本的标签。 然后将其添加到您的tableView页脚。 在页脚视图添加一个(自定义/透明)button,所以当它触及它将加载您的主数组与更多的数据,然后重新加载您的tableView。

希望这可以帮助!

做我自己

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