Swift写网易云音乐列表
时间: 2023-06-30 21:04:56 浏览: 71
要实现一个网易云音乐列表,你需要先创建一个模型来表示音乐列表中的每个元素。例如,你可以创建一个名为 `Music` 的类,该类包含歌曲的属性,如歌曲名称、歌手名称和专辑名称。
接下来,你需要在你的视图控制器中创建一个表视图,并设置其代理和数据源。然后,将每个音乐元素添加到表格中,并将其呈现为单元格。
以下是一个示例代码,用于创建一个简单的网易云音乐列表:
```swift
import UIKit
class Music {
var title: String
var artist: String
var album: String
init(title: String, artist: String, album: String) {
self.title = title
self.artist = artist
self.album = album
}
}
class MusicListViewController: UITableViewController {
var musicList: [Music] = [
Music(title: "稻香", artist: "周杰伦", album: "12新作"),
Music(title: "演员", artist: "薛之谦", album: "初学者"),
Music(title: "光年之外", artist: "邓紫棋", album: "光年之外")
]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.musicList.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let music = self.musicList[indexPath.row]
cell.textLabel?.text = music.title
cell.detailTextLabel?.text = "\(music.artist) - \(music.album)"
return cell
}
}
```
在这个示例中,你首先创建了一个 `Music` 类,来表示音乐元素。然后,在 `MusicListViewController` 中,你定义了一个包含三个 `Music` 实例的数组,在 `viewDidLoad()` 方法中注册了表格单元格,并在 `tableView(_:numberOfRowsInSection:)` 和 `tableView(_:cellForRowAt:)` 方法中将每个音乐元素添加到表格中。
相关推荐
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)