如何在 Swift 中为 iOS 应用实现 Pull-to-Refresh (UIRefreshControl)?
大家好!我是 iOS Swift 开发新手,一直搞不清楚如何为表格实现 pull-to-refresh。我看了很多教程,但大多数都已过时或使用 Objective-C。
我需要当用户向下拖动屏幕时更新 UITableView 中的数据。我创建了一个简单的项目,表格显示一个字符串数组。以下是我当前的代码:
```swift
import UIKit
class ViewController: UIViewController, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var items = ["Item 1", "Item 2", "Item 3"]
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: