是否可以从JSON获取特定元素并将其其他元素显示在collectionView上?
更广泛的问题: 这是我的JSON对象:
{
"id": 1,
"floor": 1,
"full_name": "Ernst & Young Audit LLC",
"description": "Ernst & Young («Эрнст энд Янг», в наст. время — EY) — британская аудиторско-консалтинговая компания, одна из крупнейших в мире. Основана в 1989 путём слияния американских аудиторских компаний A. C. Young, основанной Артуром Янгом в 1906 году, и Ernst & Whinney, основанной Элвином Эрнстом в 1903 году.\r\n\r\nПодробнее: http://www.ey.com/",
"logo_url": "http://########/EY_logo13.png"
}
在这里,我想引用full_name
的{{1}},description
和logo_url
,这是一个整数,其值为2,3,4,5。
例如:
如果floor
为1,我想从第1层获取值,如果为2,则从第二层获取值,依此类推。接下来,将其显示在我的CollectionView上。但是我的代码什么也没显示。任何帮助,将不胜感激!
这是我到目前为止所做的:
floor
CollectionView
var companies = [Employer]()
var companiesByFloor: [Int : [Employer]] = [:]
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Карта"
getEmployerData {
self.employerCollectionView.reloadData()
}
floorCollectionView.reloadData()
scrollView.minimumZoomScale = 1.0
scrollView.maximumZoomScale = 5.0
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return mapImage
}
func getEmployerData(completed: @escaping () -> ()) {
let url = URL(string: "http://######/employer/")
Alamofire.request(url!).responseJSON { response in
let data = response.data
do {
self.companies = try JSONDecoder().decode([Employer].self, from: data!)
for item in self.companies {
let arr = Employer(floor: item.floor, full_name: item.full_name, description: item.description, logo_url: item.logo_url)
self.companiesByFloor[item.floor] = [arr]
}
DispatchQueue.main.async {
completed()
}
}
catch let e{
print(e)
}
}
}
}
答案 0 :(得分:0)
您在方法do block
中的getEmployerData
中的代码应如下所示:
self.companies = try JSONDecoder().decode([Employer].self, from: data!)
for item in self.companies {
if self.companiesByFloor[item.floor] == nil {
self.companiesByFloor[item.floor] = [item]
} else {
self.companiesByFloor[item.floor].append(item)
}
}
答案 1 :(得分:0)
要正确显示这些部分,您必须添加一个代表这些部分的数组floors
:
var floors = [Int]()
将数组分组为字典非常容易
self.companies = try JSONDecoder().decode([Employer].self, from: data!)
self.companiesByFloor = Dictionary(grouping: self.companies, by: {$0.floor})
self.floors = self.companiesByFloor.keys.sorted()
DispatchQueue.main.async {
completed()
}
然后获得numberOfItemsInSection
,
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
let floor = floors[section]
return companiesByFloor[floor]!.count
}
您还必须实现numberOfSections
func numberOfSections(in collectionView: UICollectionView) -> Int {
return floors.count
}
要在collectionView中显示一个值,请使用类似的
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath)
let floor = floors[indexPath.section]
let companies = companiesByFloor[floor]!
let company = companies[indexPath.item]
cell.someLabel.text = company.full_name
return cell
}