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.
143 lines
4.6 KiB
143 lines
4.6 KiB
//
|
|
// SearchController.swift
|
|
// cloudmusic
|
|
//
|
|
// Created by Qihua Pan on 2020/7/3.
|
|
// Copyright © 2020 Qihua Pan. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import SwiftyJSON
|
|
import CoreData
|
|
class SearchController: UIViewController {
|
|
|
|
// 注意: Swift中mark注释的格式: MARK:-
|
|
// MARK:- 属性
|
|
let cellID = "CellIdentifier"
|
|
//关键词
|
|
@IBOutlet weak var keyword: UITextField!
|
|
|
|
//搜索结果表格
|
|
@IBOutlet weak var musicTable: UITableView!
|
|
|
|
//搜索结果
|
|
|
|
var musicList:[cloudmusic.Music]=[]
|
|
|
|
@IBOutlet weak var item: UITabBarItem!
|
|
|
|
//关键词检索歌曲
|
|
@IBAction func search(_ sender: UIButton) {
|
|
self.keyword.resignFirstResponder()
|
|
if let k=keyword{
|
|
if k.text!.count>0{
|
|
MusicRequest.search(keyword: k.text!,callback: {(json) in
|
|
debugPrint("json:\(String(describing: json))")
|
|
if let res=json{
|
|
if res["code"]==200{
|
|
self.musicList=[]
|
|
for (_,subJson):(String, JSON) in res["result"]["songs"] {
|
|
|
|
let music=self.getMusic(id: subJson["id"].int32Value, name: subJson["name"].stringValue)
|
|
|
|
debugPrint("id=\(music.id),name=\(String(describing: music.name))")
|
|
self.musicList.append(music)
|
|
}
|
|
self.musicTable.reloadData()
|
|
self.musicTable.isHidden=false
|
|
|
|
}else{
|
|
self.alertModal(message: "请求失败,请稍后再试")
|
|
}
|
|
}else{
|
|
self.alertModal(message: "请求失败,请稍后再试")
|
|
}
|
|
})
|
|
}else{
|
|
self.alertModal(message: "关键词不能为空")
|
|
}
|
|
}
|
|
}
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
// Do any additional setup after loading the view.
|
|
// 设置数据源代理
|
|
self.musicTable.dataSource = self
|
|
self.musicTable.delegate = self
|
|
|
|
self.musicTable.isHidden=true
|
|
|
|
// 注册cell
|
|
self.musicTable.register(UITableViewCell.self, forCellReuseIdentifier: cellID)
|
|
|
|
self.keyword.delegate=self
|
|
self.keyword.returnKeyType=UIReturnKeyType.done
|
|
|
|
print(self)
|
|
}
|
|
|
|
|
|
/*
|
|
// 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 SearchController: UITableViewDataSource,UITableViewDelegate{
|
|
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
return 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){
|
|
if let tabbar = self.tabBarController{
|
|
tabbar.selectedIndex=0
|
|
if let controllers=tabbar.viewControllers{
|
|
let music=self.musicList[indexPath.row]
|
|
if controllers[0] is PlayController{
|
|
let controller=controllers[0] as! PlayController
|
|
controller.play(music: music)
|
|
}
|
|
if controllers[2] is PlayListController{
|
|
let controller=controllers[2] as! PlayListController
|
|
if !music.status{
|
|
music.status=true
|
|
controller.addMusic(music: music)
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension SearchController:UITextFieldDelegate{
|
|
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
|
|
self.keyword.resignFirstResponder()
|
|
return true
|
|
}
|
|
|
|
func textFieldDidEndEditing(_ textField: UITextField) {
|
|
self.search(UIButton())
|
|
}
|
|
}
|
|
|