ios - 如果用户点击 UITableViewsectionIndex 是否可以收到通知

标签 ios uitableview

如果用户单击UITableView,我想更改其部分索引的颜色。

我可以在初始化 tableview 和任何其他状态时设置 sectionIndexColorsectionIndexTrackingBackgroundColor,但我发现没有方法可以更改单击时的索引颜色。

更新:

我的意思是较大桌面 View 中的 A-Z 侧栏。以下是一些图像(参见图 2 中的灰色条)

未选择状态:

enter image description here

所选状态: enter image description here

最佳答案

对此我只有一个相当肮脏的解决方案,因为苹果不支持公共(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/

相关文章:

ios - 检查 Apple LiveText 是否可用?

ios - 使用按钮快速循环遍历数组项

ios - 从 ContainerView 获取 subview

ios - 有没有办法以编程方式获取 iOS 中音频单元的所有参数?

ios - 如何在tableViewCell中获取准确的星星

ios - 我可以以编程方式滚动到 UIPickerView 中所需的行吗?

iphone - UITableView 垂直和水平滚动

ios - TableViewCell 为零

ios - 如何在 UITableViewCell 单元格中隐藏具有大小的按钮?

iphone - 重新排序控件未显示在 TableView 中