SectionIndexView
SectionIndexView copied to clipboard
Customizing the UITableView section index written in Swift
Overview
default | custom | image |
---|---|---|
![]() |
![]() |
![]() |
Installation
CocoaPods
pod 'SectionIndexView'
Swift Package Manager
- File > Swift Packages > Add Package Dependency
https://github.com/0xcj/SectionIndexView.git
- Select "Up to Next Major" with "2.0.1"
Manual
Drop the swift files inside of SectionIndexViewDemo/SectionIndexView into your project.
Usage
Swift
override func viewDidLoad() {
......
let titles = ["A","B","C","D","E","F","G"]
let items = titles.compactMap { (title) -> SectionIndexViewItem? in
let item = SectionIndexViewItemView.init()
item.title = title
item.indicator = SectionIndexViewItemIndicator.init(title: title)
return item
}
self.tableView.sectionIndexView(items: items)
}
Objective-C
- (void)viewDidLoad {
[super viewDidLoad];
......
NSMutableArray<UIView<SectionIndexViewItem>*> *items = [[NSMutableArray alloc]init];
NSArray *titles = @[@"A",@"B",@"C",@"D",@"E",@"F",@"G"];
for (NSString *title in titles) {
SectionIndexViewItemView *item = [[SectionIndexViewItemView alloc] init];
item.title = title
item.indicator = [[SectionIndexViewItemIndicator alloc]initWithTitle:title];
[items addObject:item];
}
[self.tableView sectionIndexViewWithItems:[NSArray arrayWithArray:items]];
}
Attention
In order to assure SectionIndexView
has correct scrolling when your navigationBar not hidden and UITableView use contentInsetAdjustmentBehavior
or automaticallyAdjustsScrollViewInsets
to adjust content. Set adjustedContentInset value equal to UITableView’s adjustment content inset
override func viewDidLoad() {
......
let navigationBarHeight = self.navigationController.navigationBar.frame.height
let statusBarHeight = UIApplication.shared.statusBarFrame.size.height
let frame = CGRect.init(x: 0, y: 0, width: width, height: height)
let tableView = UITableView.init(frame: frame, style: .plain)
let configuration = SectionIndexViewConfiguration.init()
configuration.adjustedContentInset = statusBarHeight + navigationBarHeight
tableView.sectionIndexView(items: items, configuration: configuration)
}
If you want to control the UITableView and SectionIndexView manually,you can use it like this. There is an example.
override func viewDidLoad() {
......
let indexView = SectionIndexView.init(frame: frame)
indexView.delegate = self
indexView.dataSource = self
self.view.addSubview(indexView)
}
Please see the demo for more details.
License
All source code is licensed under the License