Ale*_*lin 3 cocoa-touch objective-c key-value-observing
我开始使用键值观察,并且我正在观察的可变数组在更改字典中给出了NSIndexSets(Ordered mutable to-many).问题是表视图,据我所知,我希望我给它NSArrays充满索引.
我考虑过实现一个自定义方法将一个方法转换为另一个,但这似乎很慢,我得到的印象是,当数组发生更改时,必须有一个更好的方法来使这个表视图更新.
这是我的UITableViewDataSource的方法.
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
switch ([[change valueForKey:NSKeyValueChangeKindKey] unsignedIntValue]) {
case NSKeyValueChangeSetting:
NSLog(@"Setting Change");
break;
case NSKeyValueChangeInsertion:
NSLog(@"Insertion Change");
// How do I fit this:
NSIndexSet * indexes = [change objectForKey:NSKeyValueChangeIndexesKey];
// into this:
[self.tableView insertRowsAtIndexPaths:<#(NSArray *)#> withRowAnimation:<#(UITableViewRowAnimation)#>
// Or am I just doing it wrong?
break;
case NSKeyValueChangeRemoval:
NSLog(@"Removal Change");
break;
case NSKeyValueChangeReplacement:
NSLog(@"Replacement Change");
break;
default:
break;
}
}
Run Code Online (Sandbox Code Playgroud)
Jos*_*ell 11
这似乎很容易.使用enumerateIndexesUsingBlock:每个索引枚举索引集并将其粘贴到NSIndexPath对象中:
NSMutableArray * paths = [NSMutableArray array];
[indexes enumerateIndexesUsingBlock:^(NSUInteger index, BOOL *stop) {
[paths addObject:[NSIndexPath indexPathWithIndex:index]];
}];
[self.tableView insertRowsAtIndexPaths:paths
withRowAnimation:<#(UITableViewRowAnimation)#>];
Run Code Online (Sandbox Code Playgroud)
如果你的表视图有部分,它只是有点复杂,因为你需要获得正确的节号,并在索引路径中指定它:
NSUInteger sectionAndRow[2] = {sectionNumber, index};
[NSIndexPath indexPathWithIndexes:sectionAndRow
length:2];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3066 次 |
| 最近记录: |