【发布时间】:2025-12-14 19:10:01
【问题描述】:
我有两个文件夹,每个文件夹都包含图像、歌曲名称和 JSON 文件: 需要弄清楚如何从每个 JSON 加载数据。也许它应该是一个数组。 JSON 文件:
第二个文件夹:
{
"beatpacktwo":
{
"twoloops": [
{
"name": "Midnight",
"producer": "Stefan Guy",
"count": "32",
"genre": "R&B",
"imagename": "beatpacktwo"
}
]
}
}
第一个文件夹:
{
"beatpackone":
{
"oneloops": [
{
"name": "Away we go",
"producer": "Tubular Kingz",
"count": "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
let producer: String
let count: String
let genre: String
let imagename: String
}
public class DataLoaderTwo {
func parseJSONtwo() -> BeatPackDataTwo? {
guard let url = Bundle.main.url(forResource: "beatpacktwo", withExtension: "json") else {
print("\n-------> bundle path error")
return nil
}
do {
let jsonData = try Data(contentsOf: url)
let response = try JSONDecoder().decode(BeatPackTwo.self, from: jsonData)
print("\n-------> response: \(response)")
return response.beatpacktwo
}
catch {
print("\n====> error: \(error)" )
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
let producer: String
let count: String
let genre: String
let imagename: String
}
public class DataLoaderOne {
func parseJSONone() -> BeatPackDataOne? {
guard let url = Bundle.main.url(forResource: "beatpackone", withExtension: "json") else {
print("\n-------> bundle path error")
return nil
}
do {
let jsonData = try Data(contentsOf: url)
let response = try JSONDecoder().decode(BeatPackOne.self, from: jsonData)
print("\n-------> response: \(response)")
return response.beatpackone
}
catch {
print("\n====> error: \(error)" )
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] //Instance of our arrays from 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
}
【问题讨论】:
-
LoopTwo和LoopOne是一样的,不是吗?为什么不只使用其中之一?此外,在 JSON 中,oneloops和twoloops真的很重要吗?拥有 2 个相同但具有不同值的 JSON 不是更好吗? -
他们有不同的数据。在最后一张图片中你也看到了单元格,所以它应该从每个 json 文件中加载数据。
-
但它们具有相同的属性。我的意思是,结构是一样的。
-
那么我怎样才能做到这一点,也许你可以展示一下?
标签: ios json swift uitableview parsing