gpt4 book ai didi

ios - 在 Swift 中放置在 UIViewController 内的 tableView 上的无限滚动

转载 作者:搜寻专家 更新时间:2023-10-31 22:49:41 26 4
gpt4 key购买 nike

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

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

谢谢。

最佳答案

首先,将 tableview 拖放到 storyBoard 中的 viewController 中,然后像这样为您的 tableview 创建一个 Outlet:

@IBOutlet weak var tableView: UITableView!

并添加你的tableArray:

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

之后在你的类声明中添加 UITableViewDataSource, UITableViewDelegate,它看起来像:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

//Your code
}

之后在你的 viewDidLoad 方法中将 dataSource 和 deleget to 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
}

有关更多信息,请关注 THIS教程。

关于ios - 在 Swift 中放置在 UIViewController 内的 tableView 上的无限滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31421811/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com