LoadMoreTableViewController 1.3.2

LoadMoreTableViewController 1.3.2

测试已测试
语言语言 SwiftSwift
许可协议 MIT
发布上次发布2018 年 3 月
SPM支持 SPM

mishimayYuki MishimaYuyaAbo 维护。



  • mishimay

LoadMoreTableViewController

LoadMoreTableViewController 是一个 TableViewController,有助于您连续显示从 Web API 获取的一些数据。

基本用法

准备一个单元格

您需要准备一个在 LoadMoreTableViewController 中显示的单元格。

  • 使用 Xib 文件的方式

    1. 创建一个 xib 文件,然后将其放置在一个 Table View Cell 上。
    2. 使用 UITableView 方法 register(_:forCellReuseIdentifier:) 将单元格注册到 LoadMoreTableViewController 的表中。
      • cellReuseIdentifier 应该与 LoadMoreTableViewController 属性 public var cellReuseIdentifier 相同。
  • 使用 Storyboard 的方式

    1. 在 Storyboard 中,将一个 Table View Cell 添加到继承自 LoadMoreTableViewController 的 Table View Controller。
    2. 将 Table View Cell 的 Identifier 设置为其与 LoadMoreTableViewController 属性 public var cellReuseIdentifier 相同。

两种方式下单元格的 Identifier 都应该与 LoadMoreTableViewController 属性 public var cellReuseIdentifier 相同。默认值为 "Cell",并可配置。

应该使用 Auto Layout 设计单元格。LoadMoreTableViewController 使用了 UITableView 的自动尺寸特性,因此它将调整单元格高度。

设置闭包

  • public var fetchSourceObjects: (_ completion: @escaping (_ sourceObjects: [Any], _ hasNext: Bool) -> ()) -> ()

    • 在闭包中获取新数据。
    • 调用 completion 闭包以返回这些信息。
      • 获取的新对象(sourceObjects)。
      • 如果存在下一个加载项(hasNext)。
  • public var configureCell: (_ cell: UITableViewCell, _ row: Int) -> UITableViewCell

    • 在这个闭包中配置单元格并返回它。
    • 单元格类型与您准备的一样。

示例

import LoadMoreTableViewController

class MyTableViewController: LoadMoreTableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.registerNib(UINib(nibName: "StandardCell", bundle: nil), forCellReuseIdentifier: cellReuseIdentifier)

        fetchSourceObjects = { [weak self] completion in
            self?.request(offset: sourceObjects.count) { products in
                completion(products, true)
            }
        }
        configureCell = { [weak self] cell, row in
            cell.textLabel?.text = (self?.sourceObjects[row] as? Product)?.title
            return cell
        }
    }

}

请参阅示例项目。

其他用法

数据源

获取的数据存储在数组 public var sourceObjects: [Any] 中。您可以直接访问或操作该数组。

刷新数据

使用函数 public func refreshData(immediately immediately: Bool)

  • immediately: true
    • 立即使 tableView 空白,并从顶部开始加载数据。
    • 顶部显示加载活动指示器。
  • immediately: false
    • 在从顶部获取数据后刷新 tableView。
    • 这可以防止在 UIRefreshControl 使用时在顶部显示加载活动指示器。

其他设置

  • public func showRetryButton()

    • 将加载活动指示器更改为重试按钮。
    • 当点击重试按钮时,将开始下一次加载。
  • public static var retryText: String?

    • 更改重试按钮文本。
  • public static var retryImage: UIImage?

    • 更改重试按钮图像。
  • public var didSelectRow: ((Int) -> ())?

    • 通知选中了哪一行。

需求

Swift 3.0

安装

pod "LoadMoreTableViewController"

作者

mishimay, [email protected]

许可协议

LoadMoreTableViewController 在 MIT 许可协议下可用。更多信息请参阅 LICENSE 文件。