Ios Swift中UIViewController内的tableView无限滚动

Ios Swift中UIViewController内的tableView无限滚动,ios,swift,uitableview,uiviewcontroller,Ios,Swift,Uitableview,Uiviewcontroller,我是Swift的新手,在过去的几天里,我一直在尝试如何在放置在ViewController中的tableView中实现无限滚动。我找到的所有示例都基于TableViewController,但我的tableView放在ViewController中 实际上,我正在使用MMDrawerLayout库来获得左右滑动菜单,因此需要使用ViewController。请有人给我指引正确的方向。任何代码或样本项目将不胜感激 谢谢。首先,将tableview拖放到故事板中的viewController中,然后

我是Swift的新手,在过去的几天里,我一直在尝试如何在放置在ViewController中的tableView中实现无限滚动。我找到的所有示例都基于TableViewController,但我的tableView放在ViewController中

实际上,我正在使用MMDrawerLayout库来获得左右滑动菜单,因此需要使用ViewController。请有人给我指引正确的方向。任何代码或样本项目将不胜感激


谢谢。

首先,将tableview拖放到故事板中的viewController中,然后为tableview创建一个插座,如下所示:

@IBOutlet weak var tableView: UITableView!
并添加您的tableArray:

var items: [String] = ["We", "Heart", "Swift"]
在此之后,在类声明中添加
UITableViewDataSource、UITableViewDelegate
,它如下所示:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

 //Your code
}
之后,在
viewDidLoad
方法中整合数据源并删除self,并在其中添加该行:

self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
之后,您的方法将是:

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.dataSource = self
    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
然后添加所需的
UITableViewDataSource
方法,如下所示:

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
}

有关更多信息,请遵循教程。

要在
UITableView
底部添加无限指示器视图,请遵循以下内容

步骤1:在当前类中添加以下方法

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;

    }
步骤2:从
viewDidLoad
调用上述方法

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


        self.infinityScrollView()
    }

希望这对你有所帮助。

@bably,如果你还需要其他东西,请告诉我。