在iphone应用程序中的UITableView中自定义节索引

时间:2010-04-15 07:30:29

标签: iphone objective-c cocoa-touch uitableview

5 个答案:

答案 0 :(得分:12)

看起来标准索引视图不可自定义。

在我的应用程序中,我刚创建了自定义索引视图而不是标准视图。基本上你需要做的就是在该视图中跟踪触摸位置并相应地滚动UITableView。您可能还需要添加一些视觉效果 - 在触摸时更改视图的背景颜色并突出显示当前部分标题。

答案 1 :(得分:1)

https://github.com/Hyabusa/CMIndexBar

使用Hayabusa的这个插件。 简单替换允许设置颜色的UITableView索引

CMIndexBar *indexBar = [[CMIndexBar alloc] initWithFrame:CGRectMake(self.view.frame.size.width-35, 10.0, 28.0, self.view.frame.size.height-20)];
[indexBar setIndexes:[NSMutableArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G", nil]];
[self.view addSubview:indexBar];
[indexBar release];

代表

- (void)indexSelectionDidChange:(CMIndexBar *)IndexBar:(int)index:(NSString*)title;

答案 2 :(得分:1)

Swift版本:

tableView.sectionIndexBackgroundColor = UIColor.clearColor() //iOS7+
tableView.sectionIndexTrackingBackgroundColor = UIColor.clearColor() //iOS6+
tableView.sectionIndexColor = UIColor.redColor() //iOS6+

要自定义索引视图高度(仅UITableViewStylePlain样式):

tableView.sectionIndexMinimumDisplayRowCount = 15

答案 3 :(得分:0)

tableView.tintColor = UIColor.redColor();

会为你做这件事

答案 4 :(得分:0)

实际上没有官方Apple可以做到这一点。如果你这样做,苹果可能拒绝你的应用程序。如果您只想自定义indexBar,那么库下面可能对您有帮助。

您可以使用Swift(iOS 8+)自定义库CollectionIndexTools,您可以自定义它。

https://github.com/ReverseScale/CollectionIndexTools

以下是CollectionIndexTools

的示例代码段
lazy var collectionViewIndex: CollectionViewIndex = {
        let collectionViewIndex = CollectionViewIndex()
        collectionViewIndex.indexTitles = ["c", "v", "t", "m", "n", "w", "e", "r", "t", "y", "u", "i", "o", "p", "h", "d", "c", "b", "q"]
        collectionViewIndex.addTarget(self, action: #selector(FakeCollectionViewController.selectedIndexDidChange(_:)), for: .valueChanged)
        collectionViewIndex.translatesAutoresizingMaskIntoConstraints = false
        return collectionViewIndex
    }()