如何在表格视图单元格中快速自定义和实现滑块
Posted
技术标签:
【中文标题】如何在表格视图单元格中快速自定义和实现滑块【英文标题】:How to Customise and Implement Slider in tableview cell swift 【发布时间】:2016-08-26 12:17:52 【问题描述】:谁能建议我如何自定义 UISlider 并在 UITableViewCell 中实现它。
我看到很多自定义UISlider的代码,但我不知道如何在TableViewCell中实现它。
非常感谢 Swift 中的答案。
谢谢!
【问题讨论】:
Custom UITableViewCell with centered UISlider and two images的可能重复 【参考方案1】:您必须创建一个自定义表格视图单元格。这已经在这里得到了回答,例如Custom UITableViewCell from nib in Swift.
自定义您的单元格以拥有 UISlider 并使用您已经看过的代码进行自定义。
【讨论】:
【参考方案2】:试试吧..!可能是锻炼。
导入 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
点击操作:
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;
【讨论】:
【参考方案3】:func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> 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)
【讨论】:
以上是关于如何在表格视图单元格中快速自定义和实现滑块的主要内容,如果未能解决你的问题,请参考以下文章
拖动幻灯片时,如何在动态表格视图单元格中的标签上显示幻灯片的当前值?
删除按钮出现时自定义表格视图单元格中缺少 UISlider 宽度的动画