舉一個(gè)之前我練習(xí)過的一個(gè)例子
題干
實(shí)現(xiàn)plist文件 首先得先創(chuàng)建plist文件
創(chuàng)建方法
plist文件
之后我們需要把這個(gè)plist傳到表格上码邻,我采用的是直接放在表格上數(shù)據(jù)折剃,如果有大神可以教教我怎么直接傳數(shù)據(jù)哦!O裎荨E吕纭!
下面是代碼
根據(jù)題干 一共跳轉(zhuǎn)界面跳轉(zhuǎn)了四次
所以我們可以創(chuàng)建四個(gè)ViewController
控制器
可以把viewcontroller當(dāng)成我們的主界面
// 存放數(shù)據(jù)的字典
var cells : NSDictionary?
// 表格
var tableView:UITableView?
// UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (cells?.allKeys.count)!
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let str = "cell"
var cell = tableView.dequeueReusableCell(withIdentifier: str)
if cell == nil {
cell = UITableViewCell.init(style: .default, reuseIdentifier: str)
}
cell?.textLabel?.text = cells?.allKeys[indexPath.row] as? String;
return cell!
}
// 點(diǎn)擊按鈕跳轉(zhuǎn)
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
let sec = SecendViewController.init()
// 將值傳到下一個(gè)界面
sec.data = cells?["國內(nèi)"] as? NSDictionary
self.navigationController?.pushViewController(sec, animated: true)
}else{
let sec = SecendViewController.init()
// 將值傳到下一個(gè)界面
sec.data = cells?["國外"] as? NSDictionary
self.navigationController?.pushViewController(sec, animated: true)
}
}
override func viewDidLoad() {
super.viewDidLoad()
// 初始化表格
tableView = UITableView.init(frame: self.view.frame, style: .plain)
// 表格的協(xié)議跟代理
tableView?.delegate = self
tableView?.dataSource = self
// 添加到視圖上
self.view.addSubview(tableView!)
// 獲得plist文件到地址
let path = Bundle.main.bundlePath
let plistName:NSString = "Property List.plist"
let finalPath:NSString = (path as NSString).appendingPathComponent(plistName as String) as NSString
cells = NSDictionary(contentsOfFile:finalPath as String)
}
別忘了遵守表格協(xié)議哦
UITableViewDataSource,UITableViewDelegate
然后我們判斷跳轉(zhuǎn)后的下一個(gè)界面
// 存放數(shù)據(jù)的字典
var data : NSDictionary?
// 表格
var tableView:UITableView?
// UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (data?.allKeys.count)!
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let str = "cell"
var cell = tableView.dequeueReusableCell(withIdentifier: str)
if cell == nil {
cell = UITableViewCell.init(style: .default, reuseIdentifier: str)
}
cell?.textLabel?.text = data?.allKeys[indexPath.row] as? String;
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
let sec = ThirdViewController.init()
sec.data = data?["國內(nèi)"] as? NSDictionary
self.navigationController?.pushViewController(sec, animated: true)
}else{
let sec = ThirdViewController.init()
sec.data = data?["國外"] as? NSDictionary
self.navigationController?.pushViewController(sec, animated: true)
}
}
override func viewDidLoad() {
super.viewDidLoad()
// 初始化表格
tableView = UITableView.init(frame: self.view.frame, style: .plain)
// 表格的協(xié)議跟代理
tableView?.delegate = self
tableView?.dataSource = self
// 添加到視圖上
self.view.addSubview(tableView!)
// Do any additional setup after loading the view.
}
也是添加個(gè)表格己莺,把數(shù)據(jù)放在我們的表格上
然后就是我們的下一級(jí)
// 存放數(shù)據(jù)的字典
var data : NSDictionary?
// 表格
var tableView:UITableView?
// UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let str = "cell"
var cell = tableView.dequeueReusableCell(withIdentifier: str)
if cell == nil {
cell = UITableViewCell.init(style: .default, reuseIdentifier: str)
}
let data = ["謝霆鋒","周杰倫","劉德華"];
cell?.textLabel?.text = data[indexPath.row];
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let sec = FoutViewController.init()
// sec.data = data?["國外"] as? NSDictionary
self.navigationController?.pushViewController(sec, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
// 初始化表格
tableView = UITableView.init(frame: self.view.frame, style: .plain)
// 表格的協(xié)議跟代理
tableView?.delegate = self
tableView?.dataSource = self
// 添加到視圖上
self.view.addSubview(tableView!)
}
然后是我們最后一級(jí)
// 存放數(shù)據(jù)的字典
var data : NSDictionary?
// 表格
var tableView:UITableView?
// UITableViewDataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let str = "cell"
var cell = tableView.dequeueReusableCell(withIdentifier: str)
if cell == nil {
cell = UITableViewCell.init(style: .default, reuseIdentifier: str)
}
let data = ["因?yàn)閻鬯詯?,"只要為你活一天"];
cell?.textLabel?.text = data[indexPath.row];
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let sec = MusicViewController.init()
// sec.data = data?["國外"] as? NSDictionary
self.navigationController?.pushViewController(sec, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
// 初始化表格
tableView = UITableView.init(frame: self.view.frame, style: .plain)
// 表格的協(xié)議跟代理
tableView?.delegate = self
tableView?.dataSource = self
// 添加到視圖上
self.view.addSubview(tableView!)
最后就是我們的最后一個(gè)界面實(shí)現(xiàn)的音頻播放
首先得先下載我們MP4歌曲下載 實(shí)現(xiàn)我們音頻的播放
導(dǎo)入 頭文件 import AVFoundation
然后在viewDidLoad外面初始化一下音頻
var audioPlayer: AVAudioPlayer?
self.view.backgroundColor = .white
// 設(shè)置音樂名稱的標(biāo)簽
let lab = UILabel.init(frame: CGRect(x: 0, y: 100, width: self.view.frame.size.width, height: 30))
lab.text = "因?yàn)閻鬯詯?
lab.backgroundColor = .red
lab.textColor = .black;
lab.textAlignment = .center
self.view.addSubview(lab)
// 設(shè)置音樂
let path = Bundle.main.path(forResource: "%E5%BC%A0%E7%B4%AB%E8%B1%AA+-+%E5%8F%AF%E4%B8%8D%E5%8F%AF%E4%BB%A5", ofType: "flac")
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()
}
這樣就完成了 是不是看著很簡單
動(dòng)手操作一下吧 實(shí)力很強(qiáng)的人可以試一試在表格上直接使用plist文件上請(qǐng)求下來的數(shù)據(jù)哦奏甫!