Gunja Patel Gunja Patel - 5 months ago 38
iOS Question

Load more options with UITableView

Trying to load my data in page using pagination, I've already seen many examples but all are in Objective-C and some questions are unanswered.

My code:

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


I want this kind of output:

enter image description here

Tried to fetch selected index but it will return nil.

Answer

Create Outlets in Interface Builder for tableview and make two dynamic prototype cells give them identifiers make one cell with a button(your load more cell button) Then create action with that button that will contain the logic to load more cells!!!

now see the snipet below for reference...

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

as I have checked, It should give you the desired results.

You could also do the same in TableViewFooter.

Comments