看到有些APP在tableView右側(cè)增加了一個(gè)滾動(dòng)標(biāo)簽奠涌,并且顯示滑條所指向的cell的部分?jǐn)?shù)據(jù)娃殖。這里寫下自己的想法定庵,實(shí)現(xiàn)還是簡單的衰粹。
效果圖:
效果圖
想到的方法一:
- 剛開始打算自己創(chuàng)建一個(gè)UILabel索引標(biāo)簽锣光,然后監(jiān)聽tableView的contentOffset來實(shí)現(xiàn)索引標(biāo)簽的移動(dòng):
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// 在這里根據(jù)scrollView.contentOffset.y來改變標(biāo)簽索引的坐標(biāo)
// contentSize的高度,和屏幕高度是成固定比例的铝耻,所以可以計(jì)算出來
}
后來感覺這樣太麻煩誊爹,于是就打算監(jiān)聽scrollview內(nèi)部的導(dǎo)航view決定索引標(biāo)簽的移動(dòng)。由于是scrollView的內(nèi)部控件瓢捉,所以就通過斷點(diǎn)的方式獲取控件的成員變量名:
斷點(diǎn)
看出成員變量名如下:
成員變量名
由此可以引出方法二和三频丘。
方法二:
- 還是采用創(chuàng)建UILabel,不過是加到控制器的view上泡态,然后根據(jù)其中的_verticalScrollIndicator的左邊搂漠,來進(jìn)行相應(yīng)的移動(dòng)。
由于此標(biāo)簽索引不需要交互某弦,所以我采用了方法三桐汤。
方法三:
- 將創(chuàng)建的UILabel添加到_verticalScrollIndicator上,成為它的子控件靶壮。然后通過indexPathForRowAtPoint:獲取對(duì)應(yīng)的cell的行號(hào)怔毛。因?yàn)開verticalScrollIndicator本身是和tableView在同一個(gè)坐標(biāo)系,所以也不需要做轉(zhuǎn)換腾降。
主要代碼如下:
@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;
// 在這里根據(jù)_verticalScrollIndicator的中點(diǎn)拣度,來獲取對(duì)應(yīng)的cell行號(hào),從而可以獲取對(duì)應(yīng)行的數(shù)據(jù)來進(jìn)行顯示
self.indicatorView.text = [NSString stringWithFormat:@"%ld", [tableView indexPathForRowAtPoint:self.scrollIndicator.center].row];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
// 這里注意要在點(diǎn)擊時(shí)獲取螃壤,如果在view加載完成時(shí)設(shè)置標(biāo)簽索引的中點(diǎn)抗果,那么獲取的_verticalScrollIndicator的frame是不對(duì)的
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