All Products
Search
Document Center

Index component

Last Updated: May 18, 2021

The AUBladeView provides alphabetical index function. clicking or sliding to the letter on the alphabetical index on the left or right side of the page to trigger the event at the corresponding region.

Sample image

API description

AUBladeView.h

 
  1. //
  2. // indexBar.h
  3. //
  4. #import <Foundation/Foundation.h>
  5. #define kIndexSearchTitle @"Search"
  6. @protocol AUBladeViewDelegate;
  7. /*!
  8. @class AUBladeView
  9. @abstract UIView
  10. @discussion The alphabetical index view.
  11. */
  12. @interface AUBladeView : UIView
  13. - (id)init;
  14. - (id)initWithFrame:(CGRect)frame;
  15. - (void)clearIndex;
  16. @property (nonatomic, weak) id<AUBladeViewDelegate> delegate;
  17. @property (nonatomic, strong) UIColor *highlightedBackgroundColor;
  18. @property (nonatomic, strong) UIColor *textColor;
  19. @property (nonatomic, strong) UIFont *textFont;
  20. @property (nonatomic, strong) NSArray * iconImageNames;
  21. @property (nonatomic, strong) NSArray * iconTitles;
  22. @property (nonatomic, assign) BOOL enableSearch;
  23. @property (nonatomic, strong) NSArray * defaultIndexes;
  24. - (void)updateIndexes;
  25. @end
  26. @protocol AUBladeViewDelegate<NSObject>
  27. @optional
  28. - (void)indexSelectionDidChange:(AUBladeView *)indexBar index:(NSInteger)index title:(NSString*)title;
  29. @end

Sample code

 
  1. //
  2. // bladeViewController.m
  3. // AntUI
  4. //
  5. #import "bladeViewController.h"
  6. #import "AUBladeView.h"
  7. @interface bladeViewController ()<AUBladeViewDelegate,UITableViewDelegate,UITableViewDataSource>
  8. @property (nonatomic,strong) AUBladeView * bladeView;
  9. @property (nonatomic,strong) NSArray * sectionArr;
  10. @property (nonatomic,strong) NSArray * mainDataIndexChar;
  11. @property (nonatomic,strong) UITableView * tableView;
  12. @end
  13. @implementation bladeViewController
  14. - (void)viewDidLoad {
  15. [super viewDidLoad];
  16. self.title = @"Select a city";
  17. // Do any additional setup after loading the view.
  18. self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
  19. self.tableView.delegate = self;
  20. self.tableView.dataSource = self;
  21. [self.view addSubview:self.tableView];
  22. self.bladeView = [[AUBladeView alloc] initWithFrame:CGRectMake(self.view.frame.size.width-16.0, 80, 16.0, self.view.bounds.size.height-60)];
  23. self.bladeView.delegate = self;
  24. NSString * plistStr = [[NSBundle mainBundle] pathForResource:@"APCommonUI_ForDemo.bundle/citydict" ofType:@"plist"];
  25. NSDictionary * srcPlistDic = [NSDictionary dictionaryWithContentsOfFile:plistStr];
  26. NSMutableArray * citysList = [[NSMutableArray alloc] initWithCapacity:27];
  27. [citysList addObject:@{@"Popular":@[@"Shanghai",@"Hangzhou",@"Guangzhou",@"Beijing",@"Shenzhen"]}];
  28. NSMutableArray * indexArrList = [[NSMutableArray alloc] initWithCapacity:27];
  29. [indexArrList addObject:@"Popular"];
  30. NSArray * keyList = [srcPlistDic allKeys];
  31. NSArray * sortedList = [keyList sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
  32. return [(NSString *)obj1 compare:obj2];
  33. }];
  34. [sortedList enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  35. if (obj) {
  36. NSDictionary * tmpDic = [[NSDictionary alloc] initWithObjectsAndKeys:[srcPlistDic objectForKey:obj],obj, nil];
  37. [citysList addObject:tmpDic];
  38. [indexArrList addObject:obj];
  39. }
  40. }];
  41. self.sectionArr = citysList;
  42. self.mainDataIndexChar = indexArrList;
  43. // self.bladeView.iconTitles = secondaryIndexsTitles;
  44. // self.bladeView.iconImageNames = secondaryIndexsIcons;
  45. self.bladeView.defaultIndexes = self.mainDataIndexChar;
  46. [self.bladeView updateIndexes];
  47. [self.view addSubview:self.bladeView];
  48. self.tableView.showsVerticalScrollIndicator = NO;
  49. self.tableView.showsHorizontalScrollIndicator = NO ;
  50. [self.view bringSubviewToFront:self.bladeView];
  51. }
  52. #pragma mark -----UITableViewDelegate
  53. // Display customization
  54. // Variable height support
  55. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  56. {
  57. return 44;
  58. }// Use the estimatedHeight methods to quickly calcuate guessed values which will allow for fast load times of the table.
  59. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  60. {
  61. return 35;
  62. }
  63. #pragma mark -----UITableViewDataSource
  64. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  65. {
  66. NSDictionary * test = [self.sectionArr objectAtIndex:section] ;
  67. NSArray * valueArr = [test objectForKey:([[test allKeys] firstObject])];
  68. return [valueArr count];
  69. }
  70. - (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  71. {
  72. NSDictionary * test = [self.sectionArr objectAtIndex:section] ;
  73. return [[test allKeys] firstObject];
  74. }
  75. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  76. {
  77. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BladeTableViewCell"];
  78. if (!cell) {
  79. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BladeTableViewCell"];
  80. }
  81. NSInteger section = [indexPath section];
  82. NSInteger row = [indexPath row];
  83. NSDictionary * test = [self.sectionArr objectAtIndex:section] ;
  84. NSArray * valueArr = [test objectForKey:([[test allKeys] firstObject])];
  85. NSString * text = [valueArr objectAtIndex:row];
  86. cell.textLabel.text =text ;
  87. return cell;
  88. }
  89. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  90. {
  91. return [self.sectionArr count];
  92. }// Default is 1 if not implemented
  93. - (void)dealloc
  94. {
  95. self.tableView.delegate = nil;
  96. self.tableView.dataSource = nil;
  97. self.bladeView.delegate = nil;
  98. self.bladeView = nil;
  99. }
  100. - (void)didReceiveMemoryWarning {
  101. [super didReceiveMemoryWarning];
  102. // Dispose of any resources that can be recreated.
  103. }
  104. /*
  105. #pragma mark - Navigation
  106. // In a storyboard-based application, you will often want to do a little preparation before navigation
  107. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  108. // Get the new view controller using [segue destinationViewController].
  109. // Pass the selected object to the new view controller.
  110. }
  111. */
  112. #pragma mark ---- AUBladeViewDelegate
  113. - (void)indexSelectionDidChange:(AUBladeView *)indexBar index:(NSInteger)index title:(NSString*)title
  114. {
  115. if (self.tableView){
  116. NSInteger ret = 0;
  117. if ([title isEqualToString:kIndexSearchTitle]){
  118. [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:ret]
  119. atScrollPosition:UITableViewScrollPositionBottom
  120. animated:NO];
  121. return;
  122. }
  123. else {
  124. ret = [self findIndexSection:title];
  125. if (ret != NSNotFound) {
  126. [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:ret]
  127. atScrollPosition:UITableViewScrollPositionTop
  128. animated:NO];
  129. }
  130. }
  131. }
  132. }
  133. - (NSInteger)findIndexSection:(NSString *)title {
  134. NSInteger ret = NSNotFound;
  135. int beginIndex = 0;
  136. /*
  137. The following shows a custom section calculation rule.
  138. beginIndex += self.customSectionCount;
  139. if (self.secondarySectionCount > 0 ){
  140. for (NSInteger i = 0 ; i < [self.secondarySectionTitles count]; i++) {
  141. NSString * secondaryTitle = [self.secondarySectionTitles objectOrNilAtIndex:i];
  142. if ([secondaryTitle isEqualToString:title]) {
  143. ret = beginIndex + i;
  144. return ret;
  145. }
  146. }
  147. beginIndex += self.secondarySectionCount;
  148. }
  149. */
  150. if ([self.mainDataIndexChar count] > 0){
  151. for(NSInteger i = 0; i < [self.mainDataIndexChar count]; i++) {
  152. NSString * indexChar = [self.mainDataIndexChar objectAtIndex:i];
  153. if ([indexChar isEqualToString:title]) {
  154. ret = i;
  155. break;
  156. }
  157. }
  158. }
  159. if (ret != NSNotFound) {
  160. ret = ret + beginIndex;
  161. }
  162. return ret;
  163. }
  164. @end