我用这样的桌面视图创建了一个音乐播放列表:

当用户触摸单元音乐播放和单元格调整大小时,当视图控制器消失时,我需要使表视图保持选中。当视图被取消或消失时,是否有可能强制表视图被选中或保持不变?
代码:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! TrackCell
if selectedCellIndexPath != nil && selectedCellIndexPath == indexPath as NSIndexPath! {
selectedCellIndexPath = nil
stopSong()
cell.playImage.image = #imageLiteral(resourceName: "Play")
isPlaying = false
} else {
selectedCellIndexPath = indexPath as NSIndexPath!
cell.playImage.image = #imageLiteral(resourceName: "Pause")
isPlaying = true
playSong(name: cell.trackName.text!)
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(MusicViewController.updateDuration), userInfo: nil, repeats: true)
}
tableView.beginUpdates()
tableView.endUpdates()
if selectedCellIndexPath != nil {
tableView.scrollToRow(at: indexPath, at: .none, animated: true)
}编辑1
var player = AVAudioPlayer()
func playSong(name:String) {
//Get current index path
let indexPath:IndexPath = tableView.indexPathForSelectedRow!
let cell = tableView.cellForRow(at: indexPath as IndexPath) as! TrackCell
let url = Bundle.main.url(forResource:name , withExtension: "mp3")!
do {
player = try AVAudioPlayer(contentsOf: url)
//get slider value from music duration
cell.slider.maximumValue = Float(player.duration)
cell.slider.minimumValue = 0.0
let playerIsPlaying:Bool = UserDefaults.standard.bool(forKey: "playerIsPlaying")
if playerIsPlaying == false {
player.prepareToPlay()
player.play()
} else {
print("music is playing ")
}
} catch let error as NSError {
print(error.description)
}
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(MusicViewController.updateDuration), userInfo: nil, repeats: true)
}
func stopSong() {
player.stop()
//stop slider update
timer.invalidate()
}发布于 2017-03-08 12:54:32
尝试保存在全局变量中选择的IndexPath。然后在您的viewAppears中选择单元格a,即IndexPath。
例如:
var selectedIndexPath : IndexPath? = nil
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var tableView : UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.tableView.selectRow(at: selectedIndexPath, animated: animated, scrollPosition: UITableViewScrollPosition.none)
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedIndexPath = indexPath
}
}编辑1
在再次播放音乐时,如果单元格的音乐正在播放,则应该检查函数func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell。如果是的话,您可以将所选的UI提供给这个单元格。
试着做这样的事:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "YouCellIdentifier", for: indexPath) as? YourCellClass{
if cell.music.isPlaying{
//Selected style
}else{
//Normal style
}
return cell
}
return UITableViewCell()
}希望能帮上忙。
在这里,您可以下载一个具体示例:https://drive.google.com/file/d/0B2RAeG6eqtvCSWNVeDIzU3ZKc3M/view?usp=sharing
https://stackoverflow.com/questions/42671813
复制相似问题