Using KVO for Table Update
来源:互联网 发布:中山陵哭陵事件 知乎 编辑:程序博客网 时间:2024/05/16 09:32
http://blog.sina.com.cn/s/blog_61235faa01011y66.html
If you've followed the guidelines in Apple's Model Object Implementation Guide when creating your data model objects, you can handle your UITableView updates using KVO. This frees you from having to spread calls to reloadData, insertRowsAtIndexPaths:withRowAnimation:, ordeleteRowsAtIndexPaths:withRowAnimation: throughout your controller class wherever the data in your table might get changed.
Instead, all you have to do is observe the keypath on your data model object that holds the collection of items being displayed in the table. The easiest way to do that is in your table view controller's viewDidLoad method, like so:
[self.data addObserver:selfforKeyPath:@"items"options:0context:NULL];
Then, you just implement observeValueForKeyPath:ofObject:change:context to insert or remove rows based on changes to the observed collection. The information about which rows were inserted or deleted comes in the changes dictionary stored under the key NSKeyValueChangeIndexesK
If you do do this, make sure you remove all the other remove and insert calls, otherwise you will double-delete and double-insert, which will cause errors at runtime.
- (void)observeValueForKeyPath:(NSString *)keyPathofObject:(id)objectchange:(NSDictionary *)changecontext:(void *)context{NSIndexSet *indices = [change objectForKey:NSKeyValueChangeIndexesKey ];if (indices == nil)return; // Nothing to do// Build index paths from index setsNSUInteger indexCount = [indices count];NSUInteger buffer[indexCount];[indices getIndexes:buffer maxCount:indexCount inIndexRange:nil];NSMutableArray *indexPathArray = [NSMutableArray array];for (int i = 0; i < indexCount; i++) {NSUInteger indexPathIndices[2];indexPathIndices[0] = 0;indexPathIndices[1] = buffer[i];NSIndexPath *newPath = [NSIndexPath indexPathWithIndexes:indexPathIndices length:2];[indexPathArray addObject:newPath];}NSNumber *kind = [change objectForKey:NSKeyValueChangeKindKey];if ([kind integerValue] == NSKeyValueChangeInsertion ) // Rows were added[self.tableView insertRowsAtIndexPaths:indexPathArraywithRowAnimation:UITableViewRowAnimationFade ];else if ([kind integerValue] == NSKeyValueChangeRemoval) // Rows were removed[self.tableView deleteRowsAtIndexPaths:indexPathArraywithRowAnimation:UITableViewRowAnimationFade ];}
Once you've done this, you can forget about reloading your table. Any changes made to the underlying data - even changes made on other threads (assuming your data model class is thread-safe) will automatically trigger an animated deletion or insertion. Although the code above may look a little intimidating, this actually allows a much cleaner overall design.
- Using KVO for Table Update
- rails update table using ajax
- TRY CATCH for update table
- Batch update table name using *sp_rename*
- Using table loop for filter
- You are using safe update mode and you tried to update a table……
- Error Code: 1175. You are using safe update mode and you tried to update a table
- Mysql update error: Error Code: 1175. You are using safe update mode and you tried to update a table
- Mysql -update - Error Code: 1175. You are using safe update mode and you tried to update a table wit
- for update、for update nowait、select t.*,t.rowid from table的区别
- [MySQL] SELECT ... FOR UPDATE 的 Row Lock 與 Table Lock
- 【MySQL】select for update 的Row Lock 与Table Lock
- mysql常见问题--target table for update in FROM clause
- Hbase Table Isolation for Multi-tenancy using Region Server Grouping
- mysql update语句问题:You can't specify target table 'user' for update in FROM clause
- ErrorCode:1175.You are using safe update mode and you tried to update a table without a WHERE that u
- Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE tha
- Error Code: 1175 You are using safe update mode and you tried to update a table without a WHERE that
- 作为程序员,我想养成的几个爱好
- android应用推荐
- CAAnimation——基本动画,关键帧动画和贝塞尔路径
- sizeof进行结构体大小的判断 (包含位域),讲解详细
- JSP中forward和sendRedirect的区别
- Using KVO for Table Update
- EasySize - Dialog resizing in no time!
- 正则表达式
- 【机房系统知识小结点系列】之为什么不直接将属性设置为public,而是通过Get Set方法?
- php 访问mysql数据库验证登录
- 东软实训随笔
- 东软实训总结
- HDU4546(优先队列)
- 查未清PO