iOS UISlider 控制 UITableview 滚动

实现效果如题,应用在查找小说章节列表上
主要.m代码如下
@property (weak, nonatomic) IBOutlet UITableView *tableview;这里从xib拖个tableview就可以了 ,需要 绑定delegate和dataSource.
slider 长度位置需要根据情况微调
代码拙劣,仅供参考


#import "ViewController.h"

@interface ViewController ()

@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];
    
 
}
//返回总行数
-(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



你可能感兴趣的:(iOS UISlider 控制 UITableview 滚动)