UITableView+YYAdd.m 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // UITableView+YYAdd.m
  3. // YYKit <https://github.com/ibireme/YYKit>
  4. //
  5. // Created by ibireme on 14/5/12.
  6. // Copyright (c) 2015 ibireme.
  7. //
  8. // This source code is licensed under the MIT-style license found in the
  9. // LICENSE file in the root directory of this source tree.
  10. //
  11. #import "UITableView+YYAdd.h"
  12. #import "YYKitMacro.h"
  13. YYSYNTH_DUMMY_CLASS(UITableView_YYAdd)
  14. @implementation UITableView (YYAdd)
  15. - (void)updateWithBlock:(void (^)(UITableView *tableView))block {
  16. [self beginUpdates];
  17. block(self);
  18. [self endUpdates];
  19. }
  20. - (void)scrollToRow:(NSUInteger)row inSection:(NSUInteger)section atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated {
  21. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
  22. [self scrollToRowAtIndexPath:indexPath atScrollPosition:scrollPosition animated:animated];
  23. }
  24. - (void)insertRowAtIndexPath:(NSIndexPath *)indexPath withRowAnimation:(UITableViewRowAnimation)animation {
  25. [self insertRowsAtIndexPaths:@[indexPath] withRowAnimation:animation];
  26. }
  27. - (void)insertRow:(NSUInteger)row inSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation {
  28. NSIndexPath *toInsert = [NSIndexPath indexPathForRow:row inSection:section];
  29. [self insertRowAtIndexPath:toInsert withRowAnimation:animation];
  30. }
  31. - (void)reloadRowAtIndexPath:(NSIndexPath *)indexPath withRowAnimation:(UITableViewRowAnimation)animation {
  32. [self reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:animation];
  33. }
  34. - (void)reloadRow:(NSUInteger)row inSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation {
  35. NSIndexPath *toReload = [NSIndexPath indexPathForRow:row inSection:section];
  36. [self reloadRowAtIndexPath:toReload withRowAnimation:animation];
  37. }
  38. - (void)deleteRowAtIndexPath:(NSIndexPath *)indexPath withRowAnimation:(UITableViewRowAnimation)animation {
  39. [self deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:animation];
  40. }
  41. - (void)deleteRow:(NSUInteger)row inSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation {
  42. NSIndexPath *toDelete = [NSIndexPath indexPathForRow:row inSection:section];
  43. [self deleteRowAtIndexPath:toDelete withRowAnimation:animation];
  44. }
  45. - (void)insertSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation {
  46. NSIndexSet *sections = [NSIndexSet indexSetWithIndex:section];
  47. [self insertSections:sections withRowAnimation:animation];
  48. }
  49. - (void)deleteSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation {
  50. NSIndexSet *sections = [NSIndexSet indexSetWithIndex:section];
  51. [self deleteSections:sections withRowAnimation:animation];
  52. }
  53. - (void)reloadSection:(NSUInteger)section withRowAnimation:(UITableViewRowAnimation)animation {
  54. NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:section];
  55. [self reloadSections:indexSet withRowAnimation:animation];
  56. }
  57. - (void)clearSelectedRowsAnimated:(BOOL)animated {
  58. NSArray *indexs = [self indexPathsForSelectedRows];
  59. [indexs enumerateObjectsUsingBlock:^(NSIndexPath* path, NSUInteger idx, BOOL *stop) {
  60. [self deselectRowAtIndexPath:path animated:animated];
  61. }];
  62. }
  63. @end