我有兩個檔案夾,每個檔案夾都包含圖片、歌曲名稱和JSON檔案。
需要搞清楚,如何從每個JSON檔案中加載資料。也許它應該是一個陣列。JSON檔案:
第二個檔案夾:
{
"beatpacktwo"。
{
"twoloops": [
{
"name": "Midnight"。
"生產商": "Stefan Guy",
"計數": "32",
"genre": "R&B",
"imagename": "beatpacktwo".
}
]
}
}
第一個檔案夾:
{
"beatpackone"。
{
"oneloops": [
{
"name": "Away we go",
"生產商": "Tubular Kingz",
"計數": "28",
"genre": "Lo-fi Hip Hop",
"imagename": "beatpackone".
}
]
}
}
這里是每個檔案夾的swift檔案。BeatPackTwo
import Foundation
import UIKit
struct BeatPackTwo: Decodable {
let beatpacktwo: BeatPackDataTwo
}
struct BeatPackDataTwo: Decodable {
let oneloops: [LoopTwo]
}
struct LoopTwo: Decodable {
let name: String[/span]。
let producer: String
let count: String
let genre: String[/span]>
let imagename: String
}
public class DataLoaderTwo {
func parseJSONtwo() -> BeatPackDataTwo? {
guard let url = Bundle.main.url(forResource。"beatpacktwo", withExtension: "json") else {
print(")
-------> bundle path error")
return nil
}
do {
let jsonData = try Data(contentOf: url)
let response = try JSONDecoder() 。 decode(BeatPackTwo.self, from: jsonData)
print(")
-------> 回應。(response)")
return response.beatpacktwo
}
catch {
print(")
====> 錯誤。(錯誤)" )
return nil )
}
}
}
BeatPackOne:
import Foundation
import UIKit
struct BeatPackOne: Decodable {
let beatpackone: BeatPackDataOne
}
struct BeatPackDataOne: Decodable {
let oneloops: [LoopOne]
}
struct LoopOne: Decodable {
let name: String[/span]。
let producer: String
let count: String
let genre: String[/span]>
let imagename: String
}
public class DataLoaderOne {
func parseJSONone() -> BeatPackDataOne? {
guard let url = Bundle.main.url(forResource。"beatpackone", withExtension: "json") else {
print(")
-------> bundle path error")
return nil
}
do {
let jsonData = try Data(contentOf: url)
let response = try JSONDecoder() 。 decode(BeatPackOne.self, from: jsonData)
print(")
-------> 回應。(response)")
return response.batpackone
}
catch {
print(")
====> 錯誤。(錯誤)" )
return nil )
}
}
}
我已經嘗試在視圖控制器中創建一個實體,但現在我只能從一個JSON檔案中加載資料,需要從每一個檔案中加載,并且部分的numberofrows應該回傳檔案夾的數量。以下是我在視圖控制器中的表視圖:
extension BPLibraryViewController。UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate{
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 180 {
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1 {
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return loopsName.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: S. newOneCell, for: indexPath) as! BeatPackLibraryCell。
let loopsCount = loopsName[indexPath.row] //我們的陣列實體來自JSON。
cell.loopNameLabel.text = loopsCount.name
cell.producerNameLabel.text = loopsCount.producer
cell.loopsCountLabel.text = String( beatLoops.count)
cell.genreLabel.text = loopsCount.genre
cell.firstBeatButtonLabel.setImage(UIImage(named: loopsCount.imagename), for: .normal)
cell.delegate = self
cell.selectionStyle = .none
cell.tag = indexPath.row
if let playingCell = currentPlayingIndex, playingCell= indexPath.row {
cell.firstBtnOutlet.setImage(UIImage(named: "pauseButtonPack.png"), for:
.normal)
} else {
cell.firstBtnOutlet.setImage(UIImage(named: "playButtonPack.png" ), for:
.normal)
}
return cell
}
uj5u.com熱心網友回復:
在你的情況下,沒有理由有兩個不同結構的JSON:
讓我們改成:{
"beatPack"/span>:
{
"loops": [
{
"name": "Midnight",
"生產商": "Stefan Guy",
"計數": "32",
"genre": "R&B",
"imagename": "beatpacktwo".
}
]
}
}
而且
{
"beatPack"/span>:
{
"loops": [
{
"name": "Away we go",
"生產商": "Tubular Kingz",
"計數": "28",
"genre": "Lo-fi Hip Hop",
"imagename": "beatpackone".
}
]
}
}
我保留了beatPack & loops,但這甚至可能與保留它們無關。
然后我們的模型是:
struct TopLevel。Decodable {
let beatPack: BeatPack[/span
}
struct BeatPack: Decodable {
let loops: [Loop]
}
struct Loop: Decodable {
let name: String[/span]。
let producer: String
let count: String
let genre: String[/span]>
let imagename: String
}
然后,要決議它們:
在你的BPLibraryViewController:
var loopsName = [Loop] ()
func parseJSON(file fileName: String) -> BeatPack? {
guard let url = Bundle.main.url(forResource: fileName, withExtension: "json") else {
print(")
-------> 捆綁路徑錯誤。檔案(fileName)未找到")
return nil。
}
do {
let jsonData = try Data(contentOf: url)
let response = try JSONDecoder() 。 decode(TopLevel.self, from: jsonData)
print(")
-------> 回應。(response)")
return response.beatPack
}
catch {
print(")
====> 錯誤。(錯誤)" )
return nil )
}
}
func retrieveLoops() -> [Loop] {
let packOne = parseJSON(file: "batpackone")
let packTwo = parseJSON(file: " beatpacktwo")
var loops = [Loop]()
loops.append(contentsOf: packOne?.loops?[])
loops.append(contentsOf: packTwo?.loops ? [] )
//或者,如果你喜歡更明確的:。
// if let loopOne = packOne?.loops {
// loops.append(contentsOf: loopOne)
// }
return loops
}
func prepareInit() {
loopsNames = retrieveLoops()
tableView.reloadData()
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/311736.html
標籤:
上一篇:如何防止不同的Swift單元的標簽顯示在彼此的上面?
下一篇:表視圖單元格類的視圖顯示為nil

