针对ReactiveCocoa (Swift)实现的MutableCollectionProperty的概念
由@pepibumur实现
brew update carthage
github "gitdoapp/RAC-MutableCollectionProperty"
行添加到您的Cartfile
中carthage update
MutableCollectionProperty
的属性let property: MutableCollectionProperty<String> = MutableCollectionProperty(["test1", "test2"])
property.producer.startWithNext { newCollection in
// Do whatever you want with the new collection
// e.g. tableView.reloadData()
}
property.flatChanges.startWithNext { change in
switch change {
case Remove(Int, T)
case Insert(Int, T)
case Composite([CollectionChange])
}
}
在处理深层集合时不要依赖.flatChanges
(.flatChangesSignal
),它不会通知深层数据的变化,尽管它与平面集合相比提供了更多类型检查。使用.changes
(.changesSignal
)处理深层集合。
具有类型为MutableCollectionSection
值的集合允许改变深层元素并跟踪这些变化。
let table: MutableCollectionProperty<MutableCollectionSection<String>> =
MutableCollectionProperty([
MutableCollectionSection(["test1", "test2"]),
MutableCollectionSection(["test3", "test4"])
])
table.changes.startWithNext { change in
switch change {
case Remove([Int], Any) // [Int] — index path
case Insert([Int], Any) // [Int] — index path
case Composite([CollectionChange])
}
}
table.removeAtIndexPath([1, 1]) // will remove "test4"
您可以子类化MutableCollectionSection
以适应您的需求
class UITableViewSectionData: MutableCollectionSection<NSManagedObject> {
let name: String?
let indexTitle: String?
init(objects: [NSManagedObject], sectionName name: String?, indexTitle: String?) {
self.name = name
self.indexTitle = indexTitle
super.init(objects)
}
}
查看RACFRC的源代码。
The MIT License (MIT)
Copyright (c) 2015 GitDo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.