首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使视图消失后选择UITableview

使视图消失后选择UITableview
EN

Stack Overflow用户
提问于 2017-03-08 12:47:57
回答 1查看 561关注 0票数 0

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

当用户触摸单元音乐播放和单元格调整大小时,当视图控制器消失时,我需要使表视图保持选中。当视图被取消或消失时,是否有可能强制表视图被选中或保持不变?

代码:

代码语言:javascript
复制
 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

代码语言:javascript
复制
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()
    }
EN

回答 1

Stack Overflow用户

发布于 2017-03-08 12:54:32

尝试保存在全局变量中选择的IndexPath。然后在您的viewAppears中选择单元格a,即IndexPath

例如:

代码语言:javascript
复制
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提供给这个单元格。

试着做这样的事:

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42671813

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档