在视图消失后选择UITableview

时间:2017-03-08 12:47:57

标签: ios iphone swift xcode uitableview

我用这样的tableview创建了一个音乐播放列表:

enter image description here

当用户触摸单元格播放音乐并调整单元格大小时,我需要在视图控制器消失时使表格视图保持选中状态。当视图被解除或消失时,是否有任何可能的方法强制选择或不更改表视图?

代码:

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

1 个答案:

答案 0 :(得分:1)

尝试保存在全局变量中选择的IndexPath。然后在viewAppears中选择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