|
|
GDIIndexBar
是一个用于导航 UITableView
中节区的组件。它重新实现了 iOS 设备联系人应用中看到的索引栏,并默认设置为匹配 iOS6 和 iOS7。通过外观协议或通过子类化,可以自定义 GDIIndexBar
。
GDIIndexBar
会自动调整自身大小并定位在提供的 UITableView
的右侧。子类可以通过覆盖 GDIIndexBar
的 layoutSubviews
方法来改变这种行为。GDIIndexBar
通过设置 GDIIndexBarAlignment
值来提供索引条的自动垂直调整。
GDIIndexBar
支持直接作为子视图被添加到 UITableView
中,或者存在于不同于表的视图内,同时仍然自动定位。注意:为了作为 UITableView
的子视图正确接收触摸事件,自动将 delaysContentTouches
属性设置为 NO
。
要运行示例项目;请克隆仓库,然后首先从项目目录运行 pod install
。
示例项目中包含两个示例视图控制器。一个展示了一个 UITableViewController
子类,另一个是一个 UIViewController
,它管理着一个 UITableView
和 GDIIndexBar
作为主视图的子视图。
GDIIndexBar
可以通过代码或通过 Interface Builder 中的出口来设置进行实例化。请参考示例项目,以查看仅 IB 的实现。以下是从视图控制器的 viewDidLoad
方法中实例化索引栏的一个示例
GDIIndexBar *indexBar = [[GDIIndexBar alloc] initWithTableView:tableView];
indexBar.delegate = self;
[self.view addSubview:indexBar];
为了正确显示索引栏,其委托必须实现 <GDIIndexBarDelegate>
协议中的 numberOfIndexesForIndexBar:
和 stringForIndex:
方法。
- (NSUInteger)numberOfIndexesForIndexBar:(GDIIndexBar *)indexBar
{
return self.dataModel.sectionNames.count;
}
- (NSString *)stringForIndex:(NSUInteger)index
{
return [self.dataModel.sectionNames objectAtIndex:index];
}
要响应用户对索引栏的触摸,委托应实现以下委托方法,并告诉表视图滚动到相应的部分
- (void)indexBar:(GDIIndexBar *)indexBar didSelectIndex:(NSUInteger)index
{
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:index]
atScrollPosition:UITableViewScrollPositionTop
animated:NO];
}
GDIIndexBar
可以通过设置文本字体、文本颜色、文本阴影颜色、条 матери backgrounds 背景颜色和宽度来自定义。还可以通过使用 barBackgroundView
属性提供一个自定义的条 背景。还可以通过设置 textOffset
和 barBackgroundOffset
值分别提供文本和条背景的偏移量。
以下是通过外观协议样式化索引栏的示例。
[[GDIIndexBar appearance] setTextColor:[UIColor redColor]];
[[GDIIndexBar appearance] setTextShadowColor:[UIColor purpleColor]];
[[GDIIndexBar appearance] setTextFont:[UIFont italicSystemFontOfSize:11.5];
如需进一步自定义,子类可以重写GDIIndexBar的drawRect:
方法以执行完全自定义的绘图。
GDIIndexBar可以通过CocoaPods获取,要安装它,只需将以下行添加到您的Podfile中
pod "GDIIndexBar"
Grant Davis,[email protected]
GDIIndexBar采用MIT许可。更多信息请参阅LICENSE文件。