Search code examples
iosscrolltableviewuislider

UIslider scroll and tableview scroll ios


This is my scenario I have a UIslider and a Tableview I need to scroll tableview with slider value


Solution

  • Define UISlider in .h file as

    UISlider *slider;
    

    Here add this in viewdidload

    slider = [[UISlider alloc] initWithFrame:frame];
    [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    [slider setBackgroundColor:[UIColor clearColor]];
    slider.minimumValue = 0.0;
    slider.maximumValue =5;
    slider.continuous = YES;
    slider.value = 25.0;
    CGAffineTransform trans = CGAffineTransformMakeRotation(M_PI_2);
    slider.transform = trans;
    [slider setBackgroundColor:[UIColor clearColor]];
    [slider setThumbImage:[[UIImage imageNamed:@"XYZ.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    slider.translatesAutoresizingMaskIntoConstraints = YES;
    [temp addSubview: slider];
    

    Define the method slideraction

    -(void)sliderAction:(id)sender{NSIndexPath* indexPath1 = [NSIndexPath indexPathForRow:[(UISlider *)sender value] inSection:0];
    [yourtableview selectRowAtIndexPath:indexPath1 animated:NO scrollPosition:UITableViewScrollPositionMiddle];
    }
    

    In cellforrowatindexpath put this at the end

    UIView *selb = [[UIView alloc] initWithFrame:CGRectZero];
        [selb setBackgroundColor:[UIColor colorWithRed:255.0/255.0 green:236/255.0 blue:179/255.0 alpha:1]];
        cell.selectedBackgroundView = selb;
    

    in numberofrowsinsection array is number of rows

    slider.maximumValue = [array count];
    

    And thats it you are good to go so the end result will be like u move the slider the table view moves with a cell selected in given color