如何在tableview单元格swift中自定义和实现Slider

时间:2016-08-26 12:17:53

标签: ios swift uitableview uislider

任何人都可以建议我如何自定义UISlider并在UITableViewCell中实现它。

我看到很多代码可以自定义UISlider,但我不知道如何在TableViewCell中实现它。

斯威夫特的答案很受欢迎。

谢谢!

3 个答案:

答案 0 :(得分:1)

您必须创建自定义表格视图单元格。这已在这里得到解答,例如Custom UITableViewCell from nib in Swift

自定义您的单元格以使用UISlider并使用您已经看过的代码进行自定义。

答案 1 :(得分:1)

试试吧..!这可能是锻炼。

导入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 clickdArr = NSMutableArray()
var sliderView1: UIView!
var sliderBackground1: UIImageView!
var slider1: UISlider!    
var currentIndex : NSInteger!

在你的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行动:

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()
}

更新滑块

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()
}

在您的自定义Tableviewcell类中

@IBOutlet weak var playBtn: UIButton!
var slider1 : UISlider!
@IBOutlet weak var nameLbl: UILabel!
@IBOutlet weak var sliderVw: UIView!
在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;

答案 2 :(得分:0)

func tableView(_ tableView:UITableView,numberOfRowsInSection section:Int) - &gt; Int {

    return arrImg.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tblView.dequeueReusableCell(withIdentifier: "tblCell", for: indexPath) as! tblCell
    cell.userImg.image = UIImage(named: arrImg[indexPath.row])
    cell.slider.tag = indexPath.row
    cell.slider.addTarget(self, action: #selector(sliderChange), for: .valueChanged)
    return cell
}
@objc func sliderChange(_ sender: UISlider){

    let cell1 = tblView.cellForRow(at: IndexPath(row: sender.tag, section: 0)) as! tblCell
    cell1.userImg.alpha = CGFloat(sender.value)
}