swift获取plist文件

VC里面获取文件路径搭建界面

import UIKit

class ViewController: UIViewController ,UITableViewDelegate,UITableViewDataSource{

    var tbv:UITableView?
    var nameArr:[String] = []
    
    var dict:NSDictionary = [:]
    var data:NSMutableDictionary = [:]
    
    var marr:NSMutableArray = []
    
    
    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")
       // let plistPath = Bundle.main.path(forResource: "1", ofType: "plist")
        
        //读取属性列表文件,并转化为可变字典对象
        data = NSMutableDictionary(contentsOfFile: plistPath!)!
    
       // marr = NSMutableArray(contentsOfFile: plistPath!)!
       // print(marr)
        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 marr.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];
//        let dic:NSDictionary = marr[indexPath.row] as! NSDictionary
//        let arr:NSArray = dic.allKeys as NSArray
//        cell.textLabel?.text = arr[0] as? String;
        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)
        }
    }
}

跳第二个界面

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

}

再跳到播放界面记得导入框架AVFoundation

import UIKit
import AVFoundation

class FourVC: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    var dict:NSArray = []
    var tbv:UITableView?
    var audioPlayer: AVAudioPlayer?
  //  var nameArr:[String] = []
    var select:Bool = true
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        // 设置音乐
        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()
        
        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) {
        
        if select==true {
            audioPlayer?.play()
            select = false
        }else{
            audioPlayer?.pause()
            select = true
        }
        
       // let audioPlayer = MusicViewController()
      //  audioPlayer.playMusic(filePath)
      //  self.navigationController!.pushViewController(audioPlayer, animated: true)
    }
}

猜你喜欢

转载自blog.csdn.net/wangai9140/article/details/85120220