You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cloudmusic/cloudmusic/PlayListController.swift

109 lines
3.1 KiB

//
// PlayListController.swift
// cloudmusic
//
// Created by Qihua Pan on 2020/7/3.
// Copyright © 2020 Qihua Pan. All rights reserved.
//
import UIKit
import CoreData
class PlayListController: UIViewController {
@IBOutlet weak var table: UITableView!
@IBAction func pressClear(_ sender: UIButton) {
self.clearAll()
self.reload()
}
var musicList:[Music] = []
// : Swiftmark: MARK:-
// MARK:-
let cellID = "cell"
override func viewDidLoad() {
super.viewDidLoad()
//
table.dataSource = self
table.delegate = self
// cell
table.register(UITableViewCell.self, forCellReuseIdentifier: cellID)
}
override func viewDidAppear(_ animated: Bool) {
self.reload()
if let tabbar = self.tabBarController,let controllers=tabbar.viewControllers,controllers[0] is PlayController{
let play=controllers[0] as! PlayController
let i=IndexPath(row: play.activeIndex, section: 0)
self.table.selectRow(at: i, animated: true, scrollPosition: .none)
}
}
func reload(){
self.musicList = self.getPlayList()
self.table.reloadData()
}
func addMusic(music:Music){
self.saveContext()
self.musicList.append(music)
}
func clearList(){
self.musicList=[]
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
extension PlayListController: UITableViewDataSource,UITableViewDelegate{
// MARK:- UITableViewDataSource
// UITableViewDataSourceoption,
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.musicList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: cellID){
cell.textLabel?.text = "歌曲名:\(String(describing: musicList[indexPath.row].name!))"
return cell
}else{
return UITableViewCell(style:UITableViewCell.CellStyle.default, reuseIdentifier: cellID)
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
debugPrint("点击了\(self.musicList[indexPath.row].name!)")
if let tabbar = self.tabBarController{
if let controllers=tabbar.viewControllers{
if controllers[0] is PlayController{
let controller=controllers[0] as! PlayController
controller.play(index: indexPath.row)
}
}
}
}
}