FLEXTableViewSection.m 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. //
  2. // FLEXTableViewSection.m
  3. // FLEX
  4. //
  5. // Created by Tanner Bennett on 7/11/19.
  6. // Copyright © 2019 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXTableViewSection.h"
  9. @implementation FLEXTableViewSection
  10. + (instancetype)section:(NSInteger)section title:(NSString *)title rows:(NSArray *)rows {
  11. FLEXTableViewSection *s = [self new];
  12. s->_section = section;
  13. s->_title = title;
  14. s->_rows = rows.copy;
  15. return s;
  16. }
  17. - (instancetype)newSectionWithRowsMatchingQuery:(NSString *)query {
  18. // Find rows containing the search string
  19. NSPredicate *containsString = [NSPredicate predicateWithBlock:^BOOL(id<FLEXPatternMatching> obj, NSDictionary *bindings) {
  20. return [obj matches:query];
  21. }];
  22. NSArray *filteredRows = [self.rows filteredArrayUsingPredicate:containsString];
  23. // Only return new section if not empty
  24. if (filteredRows.count) {
  25. return [[self class] section:self.section title:self.title rows:filteredRows];
  26. }
  27. return nil;
  28. }
  29. - (NSInteger)count {
  30. return self.rows.count;
  31. }
  32. @end
  33. @implementation FLEXTableViewSection (Subscripting)
  34. - (id)objectAtIndexedSubscript:(NSUInteger)idx {
  35. return self.rows[idx];
  36. }
  37. @end