RecyclingCenter 0.0.5

RecyclingCenter 0.0.5

测试已测试
Lang语言 SwiftSwift
许可证 MIT
发布上一个发布2016年3月
SPM支持 SPM

Christopher Luu 维护。



  • 作者
  • Christopher Luu

RecyclingCenter

RecyclingCenter 是一个处理注销和入列重用对象的简单管理器。它与 UITableViewUICollectionView注销单元格的方式类似。您不是注册一个类,而是注册一个返回您的 Recyclable 类的 initHandler 闭包。

特点

  • 简单注销和入列您的 Recyclable 对象
  • 当应用程序接收内存警告时,清除未使用的对象
  • 与任何类型的对象一起工作,而不仅仅是 UI 元素

要求

  • iOS 8.0+
  • tvOS 9.0+
  • Xcode 7+

用法

对于您想要使用的每种 Recyclable 类,您需要一个 RecyclingCenter。只需初始化一个中心并为您要注销的 reuseIdentifier 注册一个 initHandler

let recyclingCenter = RecyclingCenter<RecyclableView>()
recyclingCenter.registerInitHandler({ (_) in return RecyclableView(color: .redColor()) }, forReuseIdentifier: ViewController.redReuseIdentifier)

稍后当您想要通过调用注销一个 Recyclable 对象时

let redView = recyclingCenter.dequeueObjectWithReuseIdentifier(ViewController.redReuseIdentifier, context: nil)

当您想要回收视图时,只需将其入列

recyclingCenter.enqueueObject(redView, withReuseIdentifier: ViewController.redReuseIdentifier)

看看示例项目以获得更具体的示例。尝试使用 +Red+Blue 按钮,您应该在需要创建对象时看到日志。使用 -Red-Blue 按钮移除它们,当您重新添加新的视图时,您应该看到视图正在被回收。如果您模拟内存警告,您应该看到回收的对象被冲走,因此需要创建新的对象。