diff --git a/cloudmusic.xcodeproj/project.pbxproj b/cloudmusic.xcodeproj/project.pbxproj index cdf5f64..28109af 100644 --- a/cloudmusic.xcodeproj/project.pbxproj +++ b/cloudmusic.xcodeproj/project.pbxproj @@ -3,20 +3,25 @@ archiveVersion = 1; classes = { }; - objectVersion = 50; + objectVersion = 52; objects = { /* Begin PBXBuildFile section */ 7151CAA824AFE14D004C4563 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7151CAA724AFE14D004C4563 /* AppDelegate.swift */; }; 7151CAAA24AFE14D004C4563 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7151CAA924AFE14D004C4563 /* SceneDelegate.swift */; }; - 7151CAAC24AFE14D004C4563 /* FirstViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7151CAAB24AFE14D004C4563 /* FirstViewController.swift */; }; - 7151CAAE24AFE14D004C4563 /* SecondViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7151CAAD24AFE14D004C4563 /* SecondViewController.swift */; }; 7151CAB124AFE14D004C4563 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 7151CAAF24AFE14D004C4563 /* Main.storyboard */; }; 7151CAB424AFE14D004C4563 /* cloudmusic.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 7151CAB224AFE14D004C4563 /* cloudmusic.xcdatamodeld */; }; 7151CAB624AFE152004C4563 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 7151CAB524AFE152004C4563 /* Assets.xcassets */; }; 7151CAB924AFE152004C4563 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 7151CAB724AFE152004C4563 /* LaunchScreen.storyboard */; }; 7151CAC424AFE153004C4563 /* cloudmusicTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7151CAC324AFE153004C4563 /* cloudmusicTests.swift */; }; 7151CACF24AFE153004C4563 /* cloudmusicUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7151CACE24AFE153004C4563 /* cloudmusicUITests.swift */; }; + 7151CADF24AFE612004C4563 /* PlayListController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7151CADE24AFE612004C4563 /* PlayListController.swift */; }; + 7151CAEA24AFFE6B004C4563 /* Alamofire in Frameworks */ = {isa = PBXBuildFile; productRef = 7151CAE924AFFE6B004C4563 /* Alamofire */; }; + 7151CAEC24B001EF004C4563 /* MusicRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7151CAEB24B001EE004C4563 /* MusicRequest.swift */; }; + 7151CAEE24B00ED4004C4563 /* PlayController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7151CAED24B00ED4004C4563 /* PlayController.swift */; }; + 7151CAF024B01A6B004C4563 /* SearchController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7151CAEF24B01A6B004C4563 /* SearchController.swift */; }; + 7151CAF524B02DDA004C4563 /* SwiftyJSON in Frameworks */ = {isa = PBXBuildFile; productRef = 7151CAF424B02DDA004C4563 /* SwiftyJSON */; }; + 7151CB0124B042BC004C4563 /* Alert.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7151CB0024B042BC004C4563 /* Alert.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -40,8 +45,6 @@ 7151CAA424AFE14D004C4563 /* cloudmusic.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = cloudmusic.app; sourceTree = BUILT_PRODUCTS_DIR; }; 7151CAA724AFE14D004C4563 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 7151CAA924AFE14D004C4563 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; - 7151CAAB24AFE14D004C4563 /* FirstViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FirstViewController.swift; sourceTree = ""; }; - 7151CAAD24AFE14D004C4563 /* SecondViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SecondViewController.swift; sourceTree = ""; }; 7151CAB024AFE14D004C4563 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 7151CAB324AFE14D004C4563 /* cloudmusic.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = cloudmusic.xcdatamodel; sourceTree = ""; }; 7151CAB524AFE152004C4563 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; @@ -53,6 +56,11 @@ 7151CACA24AFE153004C4563 /* cloudmusicUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = cloudmusicUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 7151CACE24AFE153004C4563 /* cloudmusicUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = cloudmusicUITests.swift; sourceTree = ""; }; 7151CAD024AFE153004C4563 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 7151CADE24AFE612004C4563 /* PlayListController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayListController.swift; sourceTree = ""; }; + 7151CAEB24B001EE004C4563 /* MusicRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MusicRequest.swift; sourceTree = ""; }; + 7151CAED24B00ED4004C4563 /* PlayController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayController.swift; sourceTree = ""; }; + 7151CAEF24B01A6B004C4563 /* SearchController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchController.swift; sourceTree = ""; }; + 7151CB0024B042BC004C4563 /* Alert.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Alert.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -60,6 +68,8 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + 7151CAF524B02DDA004C4563 /* SwiftyJSON in Frameworks */, + 7151CAEA24AFFE6B004C4563 /* Alamofire in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -105,13 +115,16 @@ children = ( 7151CAA724AFE14D004C4563 /* AppDelegate.swift */, 7151CAA924AFE14D004C4563 /* SceneDelegate.swift */, - 7151CAAB24AFE14D004C4563 /* FirstViewController.swift */, - 7151CAAD24AFE14D004C4563 /* SecondViewController.swift */, 7151CAAF24AFE14D004C4563 /* Main.storyboard */, 7151CAB524AFE152004C4563 /* Assets.xcassets */, 7151CAB724AFE152004C4563 /* LaunchScreen.storyboard */, 7151CABA24AFE152004C4563 /* Info.plist */, 7151CAB224AFE14D004C4563 /* cloudmusic.xcdatamodeld */, + 7151CADE24AFE612004C4563 /* PlayListController.swift */, + 7151CAEB24B001EE004C4563 /* MusicRequest.swift */, + 7151CAED24B00ED4004C4563 /* PlayController.swift */, + 7151CAEF24B01A6B004C4563 /* SearchController.swift */, + 7151CB0024B042BC004C4563 /* Alert.swift */, ); path = cloudmusic; sourceTree = ""; @@ -150,6 +163,10 @@ dependencies = ( ); name = cloudmusic; + packageProductDependencies = ( + 7151CAE924AFFE6B004C4563 /* Alamofire */, + 7151CAF424B02DDA004C4563 /* SwiftyJSON */, + ); productName = cloudmusic; productReference = 7151CAA424AFE14D004C4563 /* cloudmusic.app */; productType = "com.apple.product-type.application"; @@ -222,6 +239,10 @@ Base, ); mainGroup = 7151CA9B24AFE14D004C4563; + packageReferences = ( + 7151CAE824AFFE6B004C4563 /* XCRemoteSwiftPackageReference "Alamofire" */, + 7151CAF324B02DDA004C4563 /* XCRemoteSwiftPackageReference "SwiftyJSON" */, + ); productRefGroup = 7151CAA524AFE14D004C4563 /* Products */; projectDirPath = ""; projectRoot = ""; @@ -265,10 +286,13 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 7151CAAE24AFE14D004C4563 /* SecondViewController.swift in Sources */, + 7151CB0124B042BC004C4563 /* Alert.swift in Sources */, + 7151CAEE24B00ED4004C4563 /* PlayController.swift in Sources */, + 7151CADF24AFE612004C4563 /* PlayListController.swift in Sources */, 7151CAB424AFE14D004C4563 /* cloudmusic.xcdatamodeld in Sources */, - 7151CAAC24AFE14D004C4563 /* FirstViewController.swift in Sources */, + 7151CAEC24B001EF004C4563 /* MusicRequest.swift in Sources */, 7151CAA824AFE14D004C4563 /* AppDelegate.swift in Sources */, + 7151CAF024B01A6B004C4563 /* SearchController.swift in Sources */, 7151CAAA24AFE14D004C4563 /* SceneDelegate.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -593,6 +617,38 @@ }; /* End XCConfigurationList section */ +/* Begin XCRemoteSwiftPackageReference section */ + 7151CAE824AFFE6B004C4563 /* XCRemoteSwiftPackageReference "Alamofire" */ = { + isa = XCRemoteSwiftPackageReference; + repositoryURL = "https://github.com/Alamofire/Alamofire.git"; + requirement = { + kind = upToNextMajorVersion; + minimumVersion = 5.2.1; + }; + }; + 7151CAF324B02DDA004C4563 /* XCRemoteSwiftPackageReference "SwiftyJSON" */ = { + isa = XCRemoteSwiftPackageReference; + repositoryURL = "https://github.com/SwiftyJSON/SwiftyJSON.git"; + requirement = { + kind = upToNextMajorVersion; + minimumVersion = 5.0.0; + }; + }; +/* End XCRemoteSwiftPackageReference section */ + +/* Begin XCSwiftPackageProductDependency section */ + 7151CAE924AFFE6B004C4563 /* Alamofire */ = { + isa = XCSwiftPackageProductDependency; + package = 7151CAE824AFFE6B004C4563 /* XCRemoteSwiftPackageReference "Alamofire" */; + productName = Alamofire; + }; + 7151CAF424B02DDA004C4563 /* SwiftyJSON */ = { + isa = XCSwiftPackageProductDependency; + package = 7151CAF324B02DDA004C4563 /* XCRemoteSwiftPackageReference "SwiftyJSON" */; + productName = SwiftyJSON; + }; +/* End XCSwiftPackageProductDependency section */ + /* Begin XCVersionGroup section */ 7151CAB224AFE14D004C4563 /* cloudmusic.xcdatamodeld */ = { isa = XCVersionGroup; diff --git a/cloudmusic.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/cloudmusic.xcodeproj/project.xcworkspace/contents.xcworkspacedata index 7b6535d..919434a 100644 --- a/cloudmusic.xcodeproj/project.xcworkspace/contents.xcworkspacedata +++ b/cloudmusic.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -2,6 +2,6 @@ + location = "self:"> diff --git a/cloudmusic/Alert.swift b/cloudmusic/Alert.swift new file mode 100644 index 0000000..824ac6d --- /dev/null +++ b/cloudmusic/Alert.swift @@ -0,0 +1,29 @@ +// +// Alert.swift +// cloudmusic +// +// Created by Qihua Pan on 2020/7/3. +// Copyright © 2020 Qihua Pan. All rights reserved. +// + +import UIKit +import CoreData +extension UIViewController{ + func alert(message:String,title:String="警告信息",preferredStyle:UIAlertController.Style=UIAlertController.Style.alert,style:UIAlertAction.Style=UIAlertAction.Style.destructive){ + let alertController = UIAlertController(title: title, message: message, preferredStyle: preferredStyle) + + alertController.addAction(UIAlertAction(title: "确认", style:style, handler: nil)) + + self.present(alertController, animated: true, completion: nil) + } + + //获取上下文对象 + func getContext()-> NSManagedObjectContext { + //获取总代理 + let appDelegate = UIApplication.shared.delegate as! AppDelegate + //获取托管对象总管 + //let managedObjectContect = appDelegate.persistentContainer.viewContext + //返回托管对象总管 + return appDelegate.persistentContainer.viewContext + } +} diff --git a/cloudmusic/AppDelegate.swift b/cloudmusic/AppDelegate.swift index 26b40a1..b7a103e 100644 --- a/cloudmusic/AppDelegate.swift +++ b/cloudmusic/AppDelegate.swift @@ -77,6 +77,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate { } } } - + + + + } diff --git a/cloudmusic/Assets.xcassets/Pause.imageset/Contents.json b/cloudmusic/Assets.xcassets/Pause.imageset/Contents.json new file mode 100644 index 0000000..05c7bf7 --- /dev/null +++ b/cloudmusic/Assets.xcassets/Pause.imageset/Contents.json @@ -0,0 +1,21 @@ +{ + "images" : [ + { + "filename" : "Pause.png", + "idiom" : "universal", + "scale" : "1x" + }, + { + "idiom" : "universal", + "scale" : "2x" + }, + { + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/cloudmusic/Assets.xcassets/Pause.imageset/Pause.png b/cloudmusic/Assets.xcassets/Pause.imageset/Pause.png new file mode 100644 index 0000000..617045c Binary files /dev/null and b/cloudmusic/Assets.xcassets/Pause.imageset/Pause.png differ diff --git a/cloudmusic/Assets.xcassets/next.imageset/Contents.json b/cloudmusic/Assets.xcassets/next.imageset/Contents.json new file mode 100644 index 0000000..3f1e536 --- /dev/null +++ b/cloudmusic/Assets.xcassets/next.imageset/Contents.json @@ -0,0 +1,21 @@ +{ + "images" : [ + { + "filename" : "next.png", + "idiom" : "universal", + "scale" : "1x" + }, + { + "idiom" : "universal", + "scale" : "2x" + }, + { + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/cloudmusic/Assets.xcassets/next.imageset/next.png b/cloudmusic/Assets.xcassets/next.imageset/next.png new file mode 100644 index 0000000..873ae55 Binary files /dev/null and b/cloudmusic/Assets.xcassets/next.imageset/next.png differ diff --git a/cloudmusic/Assets.xcassets/play.imageset/Contents.json b/cloudmusic/Assets.xcassets/play.imageset/Contents.json new file mode 100644 index 0000000..222061f --- /dev/null +++ b/cloudmusic/Assets.xcassets/play.imageset/Contents.json @@ -0,0 +1,21 @@ +{ + "images" : [ + { + "filename" : "play.png", + "idiom" : "universal", + "scale" : "1x" + }, + { + "idiom" : "universal", + "scale" : "2x" + }, + { + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/cloudmusic/Assets.xcassets/play.imageset/play.png b/cloudmusic/Assets.xcassets/play.imageset/play.png new file mode 100644 index 0000000..54483b3 Binary files /dev/null and b/cloudmusic/Assets.xcassets/play.imageset/play.png differ diff --git a/cloudmusic/Assets.xcassets/previous.imageset/Contents.json b/cloudmusic/Assets.xcassets/previous.imageset/Contents.json new file mode 100644 index 0000000..92b4102 --- /dev/null +++ b/cloudmusic/Assets.xcassets/previous.imageset/Contents.json @@ -0,0 +1,21 @@ +{ + "images" : [ + { + "filename" : "previous.png", + "idiom" : "universal", + "scale" : "1x" + }, + { + "idiom" : "universal", + "scale" : "2x" + }, + { + "idiom" : "universal", + "scale" : "3x" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/cloudmusic/Assets.xcassets/previous.imageset/previous.png b/cloudmusic/Assets.xcassets/previous.imageset/previous.png new file mode 100644 index 0000000..b8b6aa0 Binary files /dev/null and b/cloudmusic/Assets.xcassets/previous.imageset/previous.png differ diff --git a/cloudmusic/Base.lproj/Main.storyboard b/cloudmusic/Base.lproj/Main.storyboard index 7a85710..ea904d3 100644 --- a/cloudmusic/Base.lproj/Main.storyboard +++ b/cloudmusic/Base.lproj/Main.storyboard @@ -1,102 +1,161 @@ - + + - + - - + + - - - + + + + + + + + + + + + + + + + + + + + + + - - + + + + + - - - - - - - - + + - + + + + - + - + - - + + - - - + + + - - - - - - - - - + + - + + + + + - + - + - - + + - - - - - - + + + + + + + + + + + + + + + + + + + + + + + - - + + - - + + - + - - + + + + diff --git a/cloudmusic/FirstViewController.swift b/cloudmusic/FirstViewController.swift deleted file mode 100644 index c3cb8a7..0000000 --- a/cloudmusic/FirstViewController.swift +++ /dev/null @@ -1,20 +0,0 @@ -// -// FirstViewController.swift -// cloudmusic -// -// Created by Qihua Pan on 2020/7/3. -// Copyright © 2020 Qihua Pan. All rights reserved. -// - -import UIKit - -class FirstViewController: UIViewController { - - override func viewDidLoad() { - super.viewDidLoad() - // Do any additional setup after loading the view. - } - - -} - diff --git a/cloudmusic/Info.plist b/cloudmusic/Info.plist index e7f6f58..0d2eb29 100644 --- a/cloudmusic/Info.plist +++ b/cloudmusic/Info.plist @@ -18,8 +18,15 @@ 1.0 CFBundleVersion 1 + LSApplicationCategoryType + LSRequiresIPhoneOS + NSAppTransportSecurity + + NSAllowsArbitraryLoads + + UIApplicationSceneManifest UIApplicationSupportsMultipleScenes diff --git a/cloudmusic/MusicRequest.swift b/cloudmusic/MusicRequest.swift new file mode 100644 index 0000000..a68f1a4 --- /dev/null +++ b/cloudmusic/MusicRequest.swift @@ -0,0 +1,33 @@ +// +// MusicRequest.swift +// cloudmusic +// +// Created by Qihua Pan on 2020/7/3. +// Copyright © 2020 Qihua Pan. All rights reserved. +// + +import UIKit +import Alamofire +import SwiftyJSON +//音乐接口请求 +class MusicRequest: NSObject { + + //接口服务端地址 + private static let request_host = "192.168.50.73:5000" + + //关键词搜索歌曲 + static func search(keyword:String,callback:@escaping (JSON?) -> Void ){ + let url="http://\(String(describing: request_host))/search?keywords=\(keyword)" + debugPrint("搜索请求接口地址:\(url)") + AF.request(url).responseJSON { response in + debugPrint(response) + + if let json = try? JSON(data: response.data!){ + callback(json) + }else{ + callback(nil) + } + + } + } +} diff --git a/cloudmusic/PlayController.swift b/cloudmusic/PlayController.swift new file mode 100644 index 0000000..2d68ef9 --- /dev/null +++ b/cloudmusic/PlayController.swift @@ -0,0 +1,36 @@ +// +// PlayController.swift +// cloudmusic +// +// Created by Qihua Pan on 2020/7/3. +// Copyright © 2020 Qihua Pan. All rights reserved. +// + +import UIKit + +class PlayController: UIViewController { + + + @IBOutlet weak var progress: UIProgressView! + + override func viewDidLoad() { + super.viewDidLoad() + + // Do any additional setup after loading the view. + + progress.transform = CGAffineTransform(scaleX: 1, y: 3.0) + + } + + + /* + // 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. + } + */ + +} diff --git a/cloudmusic/PlayListController.swift b/cloudmusic/PlayListController.swift new file mode 100644 index 0000000..c32f0d6 --- /dev/null +++ b/cloudmusic/PlayListController.swift @@ -0,0 +1,83 @@ +// +// PlayListController.swift +// cloudmusic +// +// Created by Qihua Pan on 2020/7/3. +// Copyright © 2020 Qihua Pan. All rights reserved. +// + +import UIKit + +class PlayListController: UIViewController { + @IBOutlet weak var table: UITableView! + + // 注意: Swift中mark注释的格式: MARK:- + // MARK:- 属性 + let cellID = "cell" + + override func viewDidLoad() { + super.viewDidLoad() + + // 设置数据源代理 + table.dataSource = self + table.delegate = self + + // 注册cell + table.register(UITableViewCell.self, forCellReuseIdentifier: cellID) + } + + + /* + // 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数据源 + // 必须实现UITableViewDataSource的option修饰的必须实现的方法,否则会报错 + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + return 20 + } + + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + /* + // ---------------------------------------------------------------- + // 使用普通方式创建cell + let cellID = "cell" + + // 1.创建cell,此时cell是可选类型 + var cell = tableView.dequeueReusableCellWithIdentifier(cellID) + + // 2.判断cell是否为nil + if cell == nil { + cell = UITableViewCell(style: .Default, reuseIdentifier: cellID) + } + + // 3.设置cell数据 + cell?.textLabel?.text = "测试数据\(indexPath.row)" + + return cell! + */ + + let cell = tableView.dequeueReusableCell(withIdentifier: cellID) + + cell?.textLabel?.text = "测试数据\(indexPath.row)" + + return cell! } + + + // MARK:- UITableViewDelegate代理 + private func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { + print("点击了\(indexPath.row)") + } +} diff --git a/cloudmusic/SearchController.swift b/cloudmusic/SearchController.swift new file mode 100644 index 0000000..1da5f32 --- /dev/null +++ b/cloudmusic/SearchController.swift @@ -0,0 +1,117 @@ +// +// 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 = "cell" + //关键词 + @IBOutlet weak var keyword: UITextField! + + //搜索结果表格 + @IBOutlet weak var musicTable: UITableView! + + //搜索结果 + + var musicList:[cloudmusic.Music]=[] + + + //关键词检索歌曲 + @IBAction func search(_ sender: UIButton) { + self.textFieldShouldReturn(keyword) + 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"] { +// debugPrint("index=\(index),subJson=\(subJson)") + let context=self.getContext() + let music = NSEntityDescription.insertNewObject(forEntityName: "Music", into: context) as! Music + music.id=subJson["id"].int64Value + music.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.alert(message: "请求失败,请稍后再试") + } + }else{ + self.alert(message: "请求失败,请稍后再试") + } + }) + }else{ + self.alert(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 + + + } + + + /* + // 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 { + let cell = tableView.dequeueReusableCell(withIdentifier: cellID) + + cell?.textLabel?.text = "歌曲名:\(String(describing: musicList[indexPath.row].name!))" + return cell! + } + + +} + +extension SearchController:UITextFieldDelegate{ + func textFieldShouldReturn(_ textField: UITextField) -> Bool { + self.keyword.resignFirstResponder() + return true + } + + func textFieldDidEndEditing(_ textField: UITextField) { + self.search(UIButton()) + } +} diff --git a/cloudmusic/SecondViewController.swift b/cloudmusic/SecondViewController.swift deleted file mode 100644 index 9f6df4c..0000000 --- a/cloudmusic/SecondViewController.swift +++ /dev/null @@ -1,20 +0,0 @@ -// -// SecondViewController.swift -// cloudmusic -// -// Created by Qihua Pan on 2020/7/3. -// Copyright © 2020 Qihua Pan. All rights reserved. -// - -import UIKit - -class SecondViewController: UIViewController { - - override func viewDidLoad() { - super.viewDidLoad() - // Do any additional setup after loading the view. - } - - -} - diff --git a/cloudmusic/cloudmusic.xcdatamodeld/cloudmusic.xcdatamodel/contents b/cloudmusic/cloudmusic.xcdatamodeld/cloudmusic.xcdatamodel/contents index 50d2514..90c75b4 100644 --- a/cloudmusic/cloudmusic.xcdatamodeld/cloudmusic.xcdatamodel/contents +++ b/cloudmusic/cloudmusic.xcdatamodeld/cloudmusic.xcdatamodel/contents @@ -1,4 +1,10 @@ - - + + + + + + + + \ No newline at end of file