Infinite scrolling through tableView hosted inside UIViewController in Swift

I am very new to Swift and over the last few days I have been trying to figure out how I can implement infinite scrolling in a tableView that fits inside a ViewController. All examples I found are based on TableViewController, but my TableView is inside ViewController.

Actually I am using MMDrawerLayout library to get left and right sliding menu, so you need to use ViewController. Please, one of us will point me in the right direction. Any projects with code or examples would be much appreciated.

Thank.

+3


source to share


2 answers


First of all, drag the table into your viewController in the storyBoard, then create an Outlet for your table view like this:

@IBOutlet weak var tableView: UITableView!

      

And add your tableArray:

var items: [String] = ["We", "Heart", "Swift"]

      

After that, add UITableViewDataSource, UITableViewDelegate

to the class declaration and look like this:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

 //Your code
}

      

After that compatible dataSource and remove yourself in your method viewDidLoad

and add the line to it:



self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

      

After that, your method will be:

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.dataSource = self
    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}

      

After that add the required method UITableViewDataSource

as shown below:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.items.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell

    cell.textLabel?.text = self.items[indexPath.row]

    return cell
}

      

For more information follow THIS .

+2


source


To add an infinity indicator view at the bottom UITableView

, then follow these steps.

Step 1 . Add the following method to your current class.

func infinityScrollView() {

        var bounds = UIScreen.mainScreen().bounds
        var width = bounds.size.width
        var height = bounds.size.height


        var footerView = UIView(frame: CGRectMake(0, 0, width, 44))
        footerView.backgroundColor=UIColor.greenColor()

        var actInd: UIActivityIndicatorView = UIActivityIndicatorView()
        actInd.center = CGPointMake(width/2 , 22)
        actInd.color = UIColor.redColor()

        footerView.addSubview(actInd)

        YOUR_TABLEVIEW_OBJECT.tableFooterView = footerView;

    }

      



Step 2 . The above method is called from viewDidLoad

.

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.


        self.infinityScrollView()
    }

      

Hope it helps you.

0


source







All Articles