测试已测试 | ✗ |
语言语言 | SwiftSwift |
许可 | MIT |
发布最后发布 | 2016年5月 |
SPM支持SPM | ✗ |
由Alexander Babaev维护。
有时您需要确定表格(集合)模型中的变更,以便用它来更新动画。当涉及多个部分时,这可能是一个更为复杂的过程。
Awesome Table Animation Calculator为这个任务提供了一个简单直观的界面。它包含表格的数据模型,并且可以计算某些变更的可动画差异(并将它们应用于UICollectionView/UITableView之后)。
安装
Code/ATableAnimation
获取源文件pod 'AwesomeTableAnimationCalculator'
github "bealex/AwesomeTableAnimationCalculator"
实现Cell和Section模型。这些模型定义了单元格的等价性(包括id等价性和内容等价性)和节。以下是一个简单的例子。
public class ASectionModelExample: ASectionModel, Equatable {
public let title:String
public init(title:String) {
self.title = title
super.init()
}
}
public func ==(lhs:ASectionModelExample, rhs:ASectionModelExample) -> Bool {
return lhs.title == rhs.title
}
单元格的实现稍微复杂一些。
class ACellModelExample: ACellModel {
var id:String
var header:String
var text:String
init(text:String, header:String) {
id = NSUUID().UUIDString
self.text = text
self.header = header
}
required init(copy:ACellModelExample) {
id = copy.id
text = copy.text
header = copy.header
}
func contentIsSameAsIn(another:ACellModelExample) -> Bool {
return text == another.text
}
}
func ==(lhs:ACellModelExample, rhs:ACellModelExample) -> Bool {
return lhs.id == rhs.id
}
连接部分和单元格的类
class ACellSectionModelExample: ACellSectionModel {
required init() {
}
func cellsHaveSameSection(one one:ACellModelExample, another:ACellModelExample) -> Bool {
return one.header == another.header
}
func createSection(forCell cell:ACellModelExample) -> ASectionModelExample {
return ASectionModelExample(title:cell.header)
}
}
创建AnimationCalculator并在其中设置cellModelComparator以正确排序单元格。
private let calculator = ATableAnimationCalculator(cellSectionModel: ACellSectionModelExample())
// somewhere in init or viewDidLoad
calculator.cellModelComparator = { left, right in
return left.header < right.header
? true
: left.header > right.header
? false
: left.text < right.text
}
然后您可以使用AnimationCalculator的方法来实现dataSource方法。
func numberOfSectionsInTableView(tableView:UITableView) -> Int {
return calculator.sectionsCount()
}
func tableView(tableView:UITableView, numberOfRowsInSection section:Int) -> Int {
return calculator.itemsCount(inSection:section)
}
func tableView(tableView:UITableView,
cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("generalCell")
cell!.textLabel!.text = calculator.item(forIndexPath:indexPath).text
return cell!
}
func tableView(tableView:UITableView, titleForHeaderInSection section:Int) -> ing? {
return calculator.section(withIndex:section).title
}
现在魔法开始。您可以简单地更改整个模型(暂时没有动画)
try! calculator.setItems([
ACellModelExample(text:"1", header:"A"),
ACellModelExample(text:"2", header:"B"),
ACellModelExample(text:"3", header:"B"),
ACellModelExample(text:"4", header:"C"),
ACellModelExample(text:"5", header:"C")
])
tableView.reloadData()
您也可以只更改单元格的子集(具有动画)
let addedItems = [
ACellModelExample(text:"2.5", header:"B"),
ACellModelExample(text:"4.5", header:"C"),
]
let itemsToAnimate = try! calculator.updateItems(addOrUpdate:addedItems, delete:
itemsToAnimate.applyTo(tableView:tableView)
如果您更改了比较器,您可以简单地重新排序模型
let itemsToAnimate = try! calculator.resortItems()
itemsToAnimate.applyTo(tableView:self.tableView)