實現(xiàn)效果如題,應用在查找小說章節(jié)列表上
主要.m代碼如下
@property (weak, nonatomic) IBOutlet UITableView *tableview;這里從xib拖個tableview就可以了 ,需要 綁定delegate和dataSource.
slider 長度位置需要根據(jù)情況微調
代碼拙劣,僅供參考
#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property(strong,nonatomic)UISlider * slider;
@property(nonatomic,retain) NSArray *listData;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSArray *array=[[NSArray alloc]initWithObjects:@"廣州",@"深圳",@"梅州",@"東莞",@"潮汕",@"肇慶",@"四會",@"佛山",@"湛江",@"江門",@"陽江",@"珠海",@"廣州",@"深圳",@"梅州",@"東莞",@"潮汕",@"肇慶",@"四會",@"佛山",@"湛江",@"江門",@"陽江",@"珠海",@"廣州",@"深圳",@"梅州",@"東莞",@"潮汕",@"肇慶",@"四會",@"佛山",@"湛江",@"江門",@"陽江",@"珠海",@"廣州",@"深圳",@"梅州",@"東莞",@"潮汕",@"肇慶",@"四會",@"佛山",@"湛江",@"江門",@"陽江",@"珠海",@"廣州",@"深圳",@"梅州",@"東莞",@"潮汕",@"肇慶",@"四會",@"佛山",@"湛江",@"江門",@"陽江",@"珠海",@"廣州",@"深圳",@"梅州",@"東莞",@"潮汕",@"肇慶",@"四會",@"佛山",@"湛江",@"江門",@"陽江",@"珠海",@"廣州",@"深圳",@"梅州",@"東莞",@"潮汕",@"肇慶",@"四會",@"佛山",@"湛江",@"江門",@"陽江",@"珠海",@"廣州",@"深圳",@"梅州",@"東莞",@"潮汕",@"肇慶",@"四會",@"佛山",@"湛江",@"江門",@"陽江",@"珠海",@"廣州",@"深圳",@"梅州",@"東莞",@"潮汕",@"肇慶",@"四會",@"佛山",@"湛江",@"江門",@"陽江",@"珠海",@"廣州",@"深圳",@"梅州",@"東莞",@"潮汕",@"肇慶",@"四會",@"佛山",@"湛江",@"江門",@"陽江",@"珠海",@"廣州",@"深圳",@"梅州",@"東莞",@"潮汕",@"肇慶",@"四會",@"佛山",@"湛江",@"江門",@"陽江",@"珠海",@"廣州",@"深圳",@"梅州",@"東莞",@"潮汕",@"肇慶",@"四會",@"佛山",@"湛江",@"江門",@"陽江",@"珠海",@"廣州",@"深圳",@"梅州",@"東莞",@"潮汕",@"肇慶",@"四會",@"佛山",@"湛江",@"江門",@"陽江",@"珠海",@"廣州",@"深圳",@"梅州",@"東莞",@"潮汕",@"肇慶",@"四會",@"佛山",@"湛江",@"江門",@"陽江",@"珠海",@"廣州",@"深圳",@"梅州",@"東莞",@"潮汕",@"肇慶",@"四會",@"佛山",@"湛江",@"江門",@"陽江",@"珠海",@"廣州",@"深圳",@"梅州",@"東莞",@"潮汕",@"肇慶",@"四會",@"佛山",@"湛江",@"江門",@"陽江",@"珠海",@"廣州",@"深圳",@"梅州",@"東莞",@"潮汕",@"肇慶",@"四會",@"佛山",@"湛江",@"江門",@"陽江",@"珠海",@"廣州",@"深圳",@"梅州",@"東莞",@"潮汕",@"肇慶",@"四會",@"佛山",@"湛江",@"江門",@"陽江",@"珠海",@"廣州",@"深圳",@"梅州",@"東莞",@"潮汕",@"肇慶",@"四會",@"佛山",@"湛江",@"江門",@"陽江",@"珠海",@"廣州",@"深圳",@"梅州",@"東莞",@"潮汕",@"肇慶",@"四會",@"佛山",@"湛江",@"江門",@"陽江",@"珠海",@"廣州",@"深圳",@"梅州",@"東莞",@"潮汕",@"肇慶",@"四會",@"佛山",@"湛江",@"江門",@"陽江",@"珠海",@"廣州",@"深圳",@"梅州",@"東莞",@"潮汕",@"肇慶",@"四會",@"佛山",@"湛江",@"江門",@"陽江",@"珠海",@"廣州",@"深圳",@"梅州",@"東莞",@"潮汕",@"肇慶",@"四會",@"佛山",@"湛江",@"江門",@"陽江",@"珠海",@"廣州",@"深圳",@"梅州",@"東莞",@"潮汕",@"肇慶",@"四會",@"佛山",@"湛江",@"江門",@"陽江",@"珠海", nil];
self.listData=array;
self.slider = [[UISlider alloc] initWithFrame:CGRectMake(40, self.view.frame.size.height/2, self.view.frame.size.height-50, 20)];
self.slider.minimumValue = 0.0;
self.slider.maximumValue = 1;
self.slider.value = 0.0;
self.slider.minimumTrackTintColor = [UIColor clearColor]; //滑輪左邊顏色慎王,如果設置了左邊的圖片就不會顯示
self.slider.maximumTrackTintColor = [UIColor clearColor]; //滑輪右邊顏色宏侍,如果設置了右邊的圖片就不會顯示
[self.slider addTarget:self action:@selector(updateValue:) forControlEvents:UIControlEventValueChanged];
self.slider.transform =CGAffineTransformMakeRotation(M_PI/2); //旋轉一下即可 -_-!!
[self.view addSubview:_slider];
}
- (void)updateValue:(id)sender {
UISlider *slider = (UISlider *)sender;
NSString * str = [NSString stringWithFormat:@"%.3f", slider.value*288];
NSLog(@"%ld",(long)[str integerValue]);
NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:[str integerValue]-1>0?[str integerValue]-1:[str integerValue] inSection:0];
[self.tableview scrollToRowAtIndexPath:scrollIndexPath
atScrollPosition:UITableViewScrollPositionTop animated:NO];
}
//返回總行數(shù)
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"count is %lu",(unsigned long)[self.listData count]);
return [self.listData count];
}
//為每一行賦值
static NSString *SimpleTableIdentifier=@"SimpleTableIdentifier";
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if(cell==nil){//如果行元素為空的話 則新建一行
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier];
}
//取得當前行
NSUInteger row=[indexPath row];
cell.textLabel.text=[_listData objectAtIndex:row]; //設置每一行要顯示的值
return cell;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//scrollView.contentOffset 會告訴你offset
NSLog(@"%f",scrollView.contentOffset.y/scrollView.contentSize.height);
self.slider.value = scrollView.contentOffset.y/scrollView.contentSize.height;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end