Issue with UITableView: Action only works every se

2020-01-30 02:00发布

I have an issue with the UITableViewController: the table view shows up but didDeselectRowAt will be called only every second time when touching a cell. Does anyone see an issue with this code?

import UIKit
import MediaPlayer


class LibraryTableViewController: UITableViewController{
    let mediaItems = MPMediaQuery.songs().items


    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
    }


    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return mediaItems!.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)  
        cell.textLabel?.text = mediaItems?[indexPath.row].title      
        return cell
    }

    override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        print("table item selected")
        OperationQueue.main.addOperation{
            self.performSegue(withIdentifier: "showPlayer", sender: self)
        }
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showPlayer"{
            let playerVC = segue.destination as! PlayerViewController
            let indexPath = tableView.indexPathForSelectedRow!
            playerVC.mediaItem = mediaItems?[indexPath.row]    
        }
    }     
}

1条回答
等我变得足够好
2楼-- · 2020-01-30 03:02

Change didDeselectRowAt to didSelectRowAt

查看更多
登录 后发表回答