UITableView右侧滚动标签

看到有些APP在tableView右侧增加了一个滚动标签,并且显示滑条所指向的cell的部分数据。这里写下自己的想法,实现还是简单的。

效果图:

效果图

想到的方法一:

  • 刚开始打算自己创建一个UILabel索引标签,然后监听tableView的contentOffset来实现索引标签的移动:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 在这里根据scrollView.contentOffset.y来改变标签索引的坐标
    // contentSize的高度,和屏幕高度是成固定比例的,所以可以计算出来
}

后来感觉这样太麻烦,于是就打算监听scrollview内部的导航view决定索引标签的移动。由于是scrollView的内部控件,所以就通过断点的方式获取控件的成员变量名:

断点

看出成员变量名如下:

成员变量名

由此可以引出方法二和三。

方法二:

  • 还是采用创建UILabel,不过是加到控制器的view上,然后根据其中的_verticalScrollIndicator的左边,来进行相应的移动。

由于此标签索引不需要交互,所以我采用了方法三。

方法三:

  • 将创建的UILabel添加到_verticalScrollIndicator上,成为它的子控件。然后通过indexPathForRowAtPoint:获取对应的cell的行号。因为_verticalScrollIndicator本身是和tableView在同一个坐标系,所以也不需要做转换。

主要代码如下:

@interface ViewController () <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) UILabel *indicatorView;
@property (weak, nonatomic) UIView *scrollIndicator;
@end

static NSString *const reuseIndentifier = @"testCell";
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.rowHeight = 150;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 200;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIndentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIndentifier];
    }
    cell.backgroundColor  = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:0.5];
    cell.textLabel.text = [NSString stringWithFormat:@"just a function test--%ld!", indexPath.row];
    
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    UITableView *tableView = (UITableView *)scrollView;
    
    // 在这里根据_verticalScrollIndicator的中点,来获取对应的cell行号,从而可以获取对应行的数据来进行显示
    self.indicatorView.text = [NSString stringWithFormat:@"%ld", [tableView indexPathForRowAtPoint:self.scrollIndicator.center].row];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    // 这里注意要在点击时获取,如果在view加载完成时设置标签索引的中点,那么获取的_verticalScrollIndicator的frame是不对的
    if (!self.indicatorView) {
        self.scrollIndicator = [self.tableView valueForKey:@"verticalScrollIndicator"];
        
        UILabel *indicatorView = [[UILabel alloc] initWithFrame:CGRectMake(-50, 0, 50, 20)];
        indicatorView.backgroundColor = [UIColor orangeColor];
        
        CGPoint center = indicatorView.center;
        center.y = self.scrollIndicator.bounds.size.height * 0.5;
        indicatorView.center = center;
        
        [self.scrollIndicator addSubview:indicatorView];
        self.indicatorView = indicatorView;
    }
}

@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,245评论 4 61
  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 9,090评论 3 38
  • 一.UITextField属性 0.enablesReturnKeyAutomatically 默认为No,如果设...
    奋斗ing0310阅读 1,688评论 0 2
  • 天空亮着 地面黑了 夏天叫着 冷风到了 四肢走着 头停下了 眼睛看着 脚迷路了 脸沉默着 身体炸了 嘴抽着烟 心脏...
    弓十三阅读 236评论 0 0
  • 我一直以为自己对时间的感知能力很强。所以,我每天都在忙碌着做着很多事。 一直也都认为还处在那种只需要人类的世界,或...
    然谷中医阅读 552评论 0 1