ios swift plist文件

ios swift plist文件
appdelegate

let vc:ViewController = ViewController()
        
        let nav = UINavigationController(rootViewController: vc)
        
        self.window?.rootViewController = nav

viewcontroller

import UIKit

class ViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource{

    var tbv:UITableView?
    var nameArr:[String] = []
    
    var dict:NSDictionary = [:]
    var data:NSMutableDictionary = [:]
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        // 生成文件的存储路径
        let plistPath = Bundle.main.path(forResource: "plist", ofType: "plist")
        
        //读取属性列表文件,并转化为可变字典对象
        data = NSMutableDictionary(contentsOfFile: plistPath!)!
    
        nameArr = data.allKeys as! [String]
        
        tbv = UITableView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
        view.addSubview(tbv!)
        
        tbv?.delegate = self
        tbv?.dataSource = self
        
        tbv?.register(UITableViewCell.self, forCellReuseIdentifier: "idCell")
        
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       // return nameArr.count
        return nameArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "idCell")!
        cell.textLabel?.text = nameArr[indexPath.row];
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if nameArr[indexPath.row]=="国外"{
            dict = data["国外"] as! NSDictionary
            let vc:SecondVC = SecondVC()
            
            vc.dict = dict
            
            self.navigationController?.pushViewController(vc, animated: true)
            
        }else{
            
            dict = data["国内"] as! NSDictionary
            
            let vc:ThreeVC = ThreeVC()
            
            vc.dict = dict
            
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
}


second

import UIKit

class SecondVC: UIViewController,UITableViewDelegate,UITableViewDataSource {

    var dict:NSDictionary = [:]
    var tbv:UITableView?
    
    var nameArr:[String] = []
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        view.backgroundColor = UIColor.white
        
        nameArr = dict.allKeys as! [String];
        
        tbv = UITableView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
        view.addSubview(tbv!)
        
        tbv?.delegate = self
        tbv?.dataSource = self
        
        tbv?.register(UITableViewCell.self, forCellReuseIdentifier: "idCell")
        
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return nameArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "idCell")!
        cell.textLabel?.text = nameArr[indexPath.row];
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if nameArr[indexPath.row]=="Britney Spears"{
            
            let arr:NSArray = dict["Britney Spears"] as! NSArray
            
            let vc:FourVC = FourVC()
            
            vc.dict = arr
            
            self.navigationController?.pushViewController(vc, animated: true)
            
        }else{
            let arr:NSArray = dict["Kelly Clarkson"] as! NSArray
            let vc:FourVC = FourVC()
            
            vc.dict = arr
            
            self.navigationController?.pushViewController(vc, animated: true)
        }
        
    }

}

third

import UIKit

class ThreeVC: UIViewController,UITableViewDelegate,UITableViewDataSource  {
    
    var dict:NSDictionary = [:]
    var tbv:UITableView?
    
    var nameArr:[String] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        view.backgroundColor = UIColor.white
        
        nameArr = dict.allKeys as! [String];
        
        tbv = UITableView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
        view.addSubview(tbv!)
        
        tbv?.delegate = self
        tbv?.dataSource = self
        
        tbv?.register(UITableViewCell.self, forCellReuseIdentifier: "idCell")

    }
    

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return nameArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "idCell")!
        cell.textLabel?.text = nameArr[indexPath.row];
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if nameArr[indexPath.row]=="大陆"{
            
            let dic:NSDictionary = dict["大陆"] as! NSDictionary
            
            let vc:FiveVC = FiveVC()
            
            vc.dict = dic
            
            self.navigationController?.pushViewController(vc, animated: true)
            
        }else{
            let dic:NSDictionary = dict["港台"] as! NSDictionary
            let vc:SixVC = SixVC()
            
            vc.dict = dic
            
            self.navigationController?.pushViewController(vc, animated: true)
        }
        
    }
    
    
}

four

import UIKit

class FourVC: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    var dict:NSArray = []
    var tbv:UITableView?
    
  //  var nameArr:[String] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        view.backgroundColor = UIColor.white
        
        tbv = UITableView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
        view.addSubview(tbv!)
        
        tbv?.delegate = self
        tbv?.dataSource = self
        
        tbv?.register(UITableViewCell.self, forCellReuseIdentifier: "idCell")
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dict.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "idCell")!
        cell.textLabel?.text = (dict[indexPath.row] as! String);
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let audioPlayer = MusicViewController()
      //  audioPlayer.playMusic(filePath)
        self.navigationController!.pushViewController(audioPlayer, animated: true)
    }
}

five

import UIKit

class FiveVC: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    var dict:NSDictionary = [:]
    var tbv:UITableView?
    
    var nameArr:[String] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        view.backgroundColor = UIColor.white
        
        nameArr = dict.allKeys as! [String];
        
        tbv = UITableView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
        view.addSubview(tbv!)
        
        tbv?.delegate = self
        tbv?.dataSource = self
        
        tbv?.register(UITableViewCell.self, forCellReuseIdentifier: "idCell")
    }
    

    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return nameArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "idCell")!
        cell.textLabel?.text = nameArr[indexPath.row];
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if nameArr[indexPath.row]=="鹿晗"{
            
            let arr:NSArray = dict["鹿晗"] as! NSArray
            
            let vc:FourVC = FourVC()
            
            vc.dict = arr
            
            self.navigationController?.pushViewController(vc, animated: true)
            
        }else{
            let arr:NSArray = dict["王凯俊"] as! NSArray
            let vc:FourVC = FourVC()
            
            vc.dict = arr
            
            self.navigationController?.pushViewController(vc, animated: true)
        }
        
    }

}

six


import UIKit

class SixVC: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    var dict:NSDictionary = [:]
    var tbv:UITableView?
    
    var nameArr:[String] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        view.backgroundColor = UIColor.white
        
        nameArr = dict.allKeys as! [String];
        
        tbv = UITableView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
        view.addSubview(tbv!)
        
        tbv?.delegate = self
        tbv?.dataSource = self
        
        tbv?.register(UITableViewCell.self, forCellReuseIdentifier: "idCell")
        
    }
    

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return nameArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "idCell")!
        cell.textLabel?.text = nameArr[indexPath.row];
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if nameArr[indexPath.row]=="周杰伦"{
            
            let arr:NSArray = dict["周杰伦"] as! NSArray
            
            let vc:FourVC = FourVC()
            
            vc.dict = arr
            
            self.navigationController?.pushViewController(vc, animated: true)
            
        }else if nameArr[indexPath.row]=="刘德华"{
            let arr:NSArray = dict["刘德华"] as! NSArray
            let vc:FourVC = FourVC()
            
            vc.dict = arr
            
            self.navigationController?.pushViewController(vc, animated: true)
        }else{
            
            let arr:NSArray = dict["谢霆锋"] as! NSArray
            let vc:FourVC = FourVC()
            vc.dict = arr
            
            self.navigationController?.pushViewController(vc, animated: true)
        }
        
    }


}

MusicViewController

import UIKit
import AVFoundation
var audioPlayer: AVAudioPlayer?
class MusicViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        // 设置音乐名称的标签
        let lab = UILabel(frame: CGRect(x: 0, y: 100, width: self.view.frame.size.width, height: 30))
        lab.text = "因为爱所以爱"
        lab.backgroundColor = .red
        lab.textColor = .black;
        lab.textAlignment = .center
        self.view.addSubview(lab)
        // 设置音乐
        let path = Bundle.main.path(forResource: "试音碟-高山流水 (古筝)", ofType: "mp3")
        let pathURL=NSURL(fileURLWithPath: path!)
        do {
            audioPlayer = try AVAudioPlayer(contentsOf: pathURL as URL)
        } catch {
            audioPlayer = nil
        }
        
        audioPlayer?.prepareToPlay()
        // 播放按钮
        let playbtn = UIButton(frame: CGRect(x: 60, y: 200, width: 100, height: 40))
        playbtn.backgroundColor = UIColor.cyan
        playbtn.setTitle("play", for: .normal)
        playbtn.setTitleColor(UIColor.white, for: .normal)
        
        // 暂停按钮
        let pausebtn = UIButton(frame: CGRect(x: 180, y: 200, width: 100, height: 40))
        pausebtn.setTitle("pause", for: .normal)
        pausebtn.setTitleColor(UIColor.white, for: .normal)
        pausebtn.backgroundColor = UIColor.cyan
        
        // 添加到视图上
        self.view.addSubview(playbtn)
        self.view.addSubview(pausebtn)
        
        // 按钮方法
        playbtn.addTarget(self, action: #selector(play), for: .touchUpInside)
        pausebtn.addTarget(self, action: #selector(pause), for: .touchUpInside)
        
        
        
        self.view.addSubview(playbtn)
        self.view.addSubview(pausebtn)
        
        // Do any additional setup after loading the view.
    }
    @objc func play(){
        audioPlayer?.play()
    }
    
    @objc func pause(){
        audioPlayer?.pause()
    }
    
   
    }