整理数据
- (NSString *)getFirstLetter {
NSString *words = [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (words.length == 0) {
return nil;
}
NSString *result = nil;
unichar firstLetter = [words characterAtIndex:0];
int index = firstLetter - HANZI_START;
if (index >= 0 && index <= HANZI_COUNT) {
result = [NSString stringWithFormat:@"%c", firstLetterArray[index]];
} else if ((firstLetter >= 'a' && firstLetter <= 'z')
|| (firstLetter >= 'A' && firstLetter <= 'Z')) {
result = [NSString stringWithFormat:@"%c", firstLetter];
} else {
result = @"#";
}
return [result uppercaseString];
}
- 将一个字符串数组按照拼音首字母规则进行重组排序, 返回重组后的数组
- (NSArray *)arrayWithPinYinFirstLetterFormat {
if (![self count]) {
return [NSMutableArray array];
}
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:[NSMutableArray array] forKey:@"#"];
for (int i = 'A'; i <= 'Z'; i++) {
[dict setObject:[NSMutableArray array]
forKey:[NSString stringWithUTF8String:(const char *)&i]];
}
for (NSDictionary *dic in self) {
NSString *words = dic[@"title"];
NSString *firstLetter = [words getFirstLetter];
NSMutableArray *array = dict[firstLetter];
[array addObject:dic];
}
NSMutableArray *resultArray = [NSMutableArray array];
for (int i = 'A'; i <= 'Z'; i++) {
NSString *firstLetter = [NSString stringWithUTF8String:(const char *)&i];
NSMutableArray *array = dict[firstLetter];
if ([array count]) {
[array sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSString *word1 = obj1[@"title"];
NSString *word2 = obj2[@"title"];
return [word1 localizedCompare:word2];
}];
NSDictionary *resultDict = @{@"firstLetter": firstLetter,
@"content": array};
[resultArray addObject:resultDict];
}
}
if ([dict[@"#"] count]) {
NSMutableArray *array = dict[@"#"];
[array sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSString *word1 = obj1[@"title"];
NSString *word2 = obj2[@"title"];
return [word1 localizedCompare:word2];
}];
NSDictionary *resultDict = @{@"firstLetter": @"#",
@"content": array};
[resultArray addObject:resultDict];
}
return resultArray;
}
- 将没有排序的数组按字母首字母的顺序排列, 生成新的排序数组:
self.firstLitterArray = (self.bankArray! as NSArray).withPinYinFirstLetterFormat() as! [[String:Any]]
UITableView协议
- 实现UITableViewDelegate,UITableViewDataSource以下的协议:
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 35
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 35))
view.backgroundColor = UIColor.init(red: 212/255.0, green: 212/255.0, blue: 212/255.0, alpha: 1)
let label = UILabel.init(frame: CGRect.init(x: 16, y: 0, width: 15, height: 35))
label.text = (self.firstLitterArray[section]["firstLetter"] as! String)
label.textAlignment = .center
label.textColor = UIColor.init(red: 153/255.0, green: 153/255.0, blue: 153/255.0, alpha: 1)
label.font = UIFont.boldSystemFont(ofSize: 14)
view.addSubview(label)
return view
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return (self.firstLitterArray[section]["firstLetter"] as! String)
}
func sectionIndexTitles(for tableView: UITableView) -> [String]? {
var title : [String] = []
for i in 0 ..< self.firstLitterArray.count {
title.append(self.firstLitterArray[i]["firstLetter"] as! String)
}
return title
}
func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
if title == UITableView.indexSearch {
return NSNotFound
}
return UILocalizedIndexedCollation.current().section(forSectionIndexTitle: index)
}
效果展示
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/29af4f483f4eeb778d2552965b821f35.gif)
完整示例
Swift之UITableView新增“索引”数组快速定位查找数据