Dhaksha Dhaksha - 3 months ago 29
iOS Question

How to Customise and Implement Slider in tableview cell swift

Can anyone suggest me how to customise an UISlider and implement it in an UITableViewCell.

I saw many codes to customise UISlider, but i don't know how to implement it in a TableViewCell.

Answers in Swift well appreciated.

Thanks!

Answer

Try it..! It may be workout.

import AVFoundation

var player = AVPlayer()
var rate = Float()
var timer = NSTimer()
var tag: NSInteger!
var nameArr = NSMutableArray()
var urlArr = NSMutableArray()
@IBOutlet weak var songsTbl: UITableView!

// Slider
@IBOutlet weak var sliderTimer: UISlider!
var sliderView: UIView!
var sliderBackground: UIImageView!
var slider: UISlider!
var clickdArr = NSMutableArray()
var sliderView1: UIView!
var sliderBackground1: UIImageView!
var slider1: UISlider!    
var currentIndex : NSInteger!

In your cellForRowAtIndexPath:

var cell : CurrentTuneCell! = tableView.dequeueReusableCellWithIdentifier("CurrentTuneCell") as! CurrentTuneCell
    if(cell == nil)
    {
        cell = NSBundle.mainBundle().loadNibNamed("CurrentTuneCell", owner: self, options: nil)[0] as! CurrentTuneCell;
    }

    cell.selectionStyle = UITableViewCellSelectionStyle.None
    cell.nameLbl.text = "\(self.nameArr[indexPath.row])"
    cell.playBtn.addTarget(self, action: #selector(clickPlay), forControlEvents: .TouchUpInside)
    cell.playBtn.tag = indexPath.row

    if(clickdArr[indexPath.row] as! String == "1")
    {
        cell.playBtn.setTitle("Stop", forState: UIControlState.Normal)
        cell.sliderVw.hidden = false;

        let duration: CMTime = self.player.currentItem!.asset.duration
        let seconds: Float64 = CMTimeGetSeconds(duration)
        let someFloat = Float(seconds)

        let crntDuration: CMTime = self.player.currentTime()
        let crntSeconds: Float64 = CMTimeGetSeconds(crntDuration)
        let myIntValue:Int = Int(crntSeconds)
        cell.nameLbl.text = "\(myIntValue)"

        let crntFloat = Float(crntSeconds)
        cell.slider1.value = crntFloat;
        cell.slider1.maximumValue = someFloat;

        cell.slider1.addTarget(self, action: #selector(sliderValueChange), forControlEvents: .ValueChanged)            
    }
    else
    {
        cell.playBtn.setTitle("Play", forState: UIControlState.Normal)

        cell.sliderVw.hidden = true;
    }
    return cell

CLick Action:

func clickPlay(sender: UIButton)
{
    tag = sender.tag

    for(var i=0;i<clickdArr.count;i++)
    {
        if(sender.tag == i)
        {
            let url = "\(self.urlArr[sender.tag])"
            let playerItem = AVPlayerItem( URL:NSURL( string:url )! )
            player = AVPlayer(playerItem:playerItem)

            currentIndex = tag;

            if(self.clickdArr[sender.tag] as! String == "0")
            {
                timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(updateSlider), userInfo: nil, repeats: true)

                player.rate = 1.0;
                rate = 1.0;
                player.pause()
                player.rate = rate;
                player.play()

                clickdArr.replaceObjectAtIndex(i, withObject: "1")
            }
            else
            {
                player.rate = 1.0;
                rate = 1.0;
                player.pause()

                clickdArr .replaceObjectAtIndex(i, withObject: "0")
            }
        }
        else
        {
            clickdArr .replaceObjectAtIndex(i, withObject: "0")
        }
    }

    self.songsTbl.reloadData()
}

Update Slider

func updateSlider()
{        
         self.songsTbl.reloadData()        
}

func sliderValueChange(sender: UISlider)
{
    let myIntValue:Int64 = Int64(sender.value)

    let timeMake = CMTimeMake(myIntValue,1)

    player.seekToTime(timeMake)
    player.play()
}

And in your Custom Tableviewcell class

@IBOutlet weak var playBtn: UIButton!
var slider1 : UISlider!
@IBOutlet weak var nameLbl: UILabel!
@IBOutlet weak var sliderVw: UIView!

in awakeFromNib

   let sliderView1 = UIView(frame: CGRectMake(0, 0, self.frame.size.width, self.frame.size.height))
    self.sliderVw .addSubview(sliderView1)

    let trackImage = UIImage(named: "slider.png")
    let sliderBackground1 = UIImageView(image: trackImage!)
    sliderBackground1.frame = CGRectMake(0, 0, self.sliderVw.frame.size.width, self.sliderVw.frame.size.height)
    slider1 = UISlider(frame: CGRectMake(0, 0, self.sliderVw.frame.size.width, self.sliderVw.frame.size.height))
    let sliderFrame = slider1.frame
    slider1.frame = sliderFrame
    slider1.center = sliderBackground1.center
    slider1.setMinimumTrackImage(UIImage(named: "slider.png")!, forState: .Normal)
    slider1.setMaximumTrackImage(UIImage(named: "blackBox.png")!, forState: .Normal)
    let thumbImage = UIImage(named: "round.png")!
    slider1.setThumbImage(thumbImage, forState: .Normal)
    slider1.minimumValue = 0.0
    slider1.maximumValue = 1.0
    slider1.continuous = true
    slider1.value = 0.0
    sliderView1.addSubview(slider1)

    self.slider1.maximumValue = 1.0;
    self.slider1.minimumValue = 1.0;