整理數據
- (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
= .centerlabel
.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
)}
效果展示
完整示例
Swift之UITableView新增“索引”數組快速定位查找數據
總結
以上是生活随笔為你收集整理的Swift之实现表格UITableView数据首字母顺序排列展示并添加“索引”快速定位查找功能的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。