bably
bably

Reputation: 1105

Infinite scroll on tableView placed inside a UIViewController in Swift

I am very new to Swift and for the last few days I have been trying to figure out how I can implement infinite scroll in tableView which is placed inside a ViewController. All the examples I have found are based on TableViewController but my tableView is placed inside a ViewController.

Actually I am using the library MMDrawerLayout to get a left and right sliding menu so need to use a ViewController. Please some one guide me in the right direction. Any code or sample projects will be much appreciated.

Thanks.

Upvotes: 3

Views: 1212

Answers (2)

Jatin Patel - JP
Jatin Patel - JP

Reputation: 3733

To Add infinity indicator view in bottom of your UITableView then followed the following things.

Step 1 : Add following method in 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 : Called the above method from viewDidLoad.

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


        self.infinityScrollView()
    }

Hope this help you.

Upvotes: 0

Dharmesh Kheni
Dharmesh Kheni

Reputation: 71852

First of all drag and drop tableview into your viewController in storyBoard after that create an Outlet for your tableview like this:

@IBOutlet weak var tableView: UITableView!

And add your tableArray:

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

After that add UITableViewDataSource, UITableViewDelegate at your class declaration and it look like:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

 //Your code
}

After that conform dataSource and deleget to self in your viewDidLoad method and add thi line in 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 required method of 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 Info follow THIS tutorial.

Upvotes: 2

Related Questions