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()
    
    
    // 生成文件的存储路径
    let plistPath = Bundle.main.path(forResource: \"plist\", ofType: \"plist\")
   
    
    //读取属性列表文件,并转化为可变字典对象
    data = NSMutableDictionary(contentsOfFile: plistPath!)!

   
    nameArr = data.allKeys as! [String]
    
    tbv = UITableView( : 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]==\"国外\"{
        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)
    }
}

}


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

}

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

}

import AVFoundation

class FourVC: UIViewController,UITableViewDelegate,UITableViewDataSource {

var dict:NSArray = []
var tbv:UITableView?
var audioP : AVAudioP ?

var select:Bool = true

override func viewDidLoad() {
    super.viewDidLoad()

    
    
    // 设置音乐
    let path = Bundle.main.path(forResource: \"试音碟-高山流水 (古筝)\", ofType: \"mp3\")
    let pathURL=NSURL(fileURLWithPath: path!)
    do {
        audioP  = try AVAudioP (contentsOf: pathURL as URL)
    } catch {
        audioP  = nil
    }
    
    audioP ?.prepareToPlay()
    
    view.backgroundColor = UIColor.white
    
    tbv = UITableView( : 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 {
        audioP ?.play()
        select = false
    }else{
        audioP ?.pause()
        select = true
    }
    
  
}

}

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

}

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

}

播放音乐
import AVFoundation
var audioP : AVAudioP ?
class MusicViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = .white
    // 设置音乐名称的标签
    let lab = UILabel( : CGRect(x: 0, y: 100, width: self.view. .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 {
        audioP  = try AVAudioP (contentsOf: pathURL as URL)
    } catch {
        audioP  = nil
    }
    
    audioP ?.prepareToPlay()
    // 播放按钮
    let playbtn = UIButton( : CGRect(x: 60, y: 200, width: 100, height: 40))
    playbtn.backgroundColor = UIColor.cyan
    playbtn.set (\"play\", for: .normal)
    playbtn.set Color(UIColor.white, for: .normal)
    
    // 暂停按钮
    let pausebtn = UIButton( : CGRect(x: 180, y: 200, width: 100, height: 40))
    pausebtn.set (\"pause\", for: .normal)
    pausebtn.set Color(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(){
    audioP ?.play()
}

@objc func pause(){
    audioP ?.pause()
}


}

\"plist\"

收藏 打印