如果用户单击UITableView
,我想更改其部分索引的颜色。
我可以在初始化 tableview 和任何其他状态时设置 sectionIndexColor
和 sectionIndexTrackingBackgroundColor
,但我发现没有方法可以更改单击时的索引颜色。
更新:
我的意思是较大桌面 View 中的 A-Z 侧栏。以下是一些图像(参见图 2 中的灰色条)
未选择状态:
所选状态:
最佳答案
对此我只有一个相当肮脏的解决方案,因为苹果不支持公共(public) api。我的实现工作得很好,如果将来某些 api 发生变化,应该不会崩溃(只是不再工作了)。我对 UITableView
进行子类化,并在将其作为 subview 添加到表中时将我自己的回调添加到sectionIndex View 中。
@interface MyTableView : UITableView
@end
@implementation MyTableView
- (void)selectionStarted {
[self setSectionIndexColor:[UIColor greenColor]];
}
- (void)selectionStoped {
[self setSectionIndexColor:[UIColor redColor]];
}
- (void)addSubview:(UIView *)view {
[super addSubview:view];
if ([NSStringFromClass([view class]) isEqualToString:@"UITableViewIndex"] && [view isKindOfClass:[UIControl class]]) {
UIControl *cont = (UIControl *)view;
[cont addTarget:self action:@selector(selectionStarted) forControlEvents:UIControlEventTouchDown];
[cont addTarget:self action:@selector(selectionStoped) forControlEvents:UIControlEventTouchUpInside];
[cont addTarget:self action:@selector(selectionStoped) forControlEvents:UIControlEventTouchUpOutside];
[cont addTarget:self action:@selector(selectionStoped) forControlEvents:UIControlEventTouchCancel];
}
}
@end
关于ios - 如果用户点击 UITableViewsectionIndex 是否可以收到通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17858194/