我有像这样的JSON文件
{
"dicts": [
{
"main": "",
"note1": "",
"note2": "",
"note3": "",
"note4": ""
},
{
"main": "",
"note1": "",
"note2": "",
"note3": "",
"note4": "",
"note5": ""
},
{
"main": "",
"note1": "",
"note2": ""
}
]
}
我的应用每个view controllers
有collection view
个collection view
。在第一个main
中,我将为每个cell
显示JSON数据main
,当我点击note
时,我无法为相应的{{1}设置main
数据每个cell
中的内容。我只能调用任何一个note
值。
型号:
struct SecPage {
var note1:String?
var note1:String?
var note2:String?
var note3:String?
var note4:String?
var note5:String?
}
static func downSec () -> [SecPage] {
let jsonFile = Bundle.main.url(forResource: "info", withExtension: "json")
let jsonData = try? Data(contentsOf: jsonFile!)
var noteArray = [SecPage]()
do{
if let jsonResult = try JSONSerialization.jsonObject(with: jsonData!, options: .mutableContainers) as? Dictionary<String,AnyObject>{
let name = jsonResult["dicts"] as? [Dictionary<String,AnyObject>]
for note in name!{
let note1 = note["note1"] as? String
let note2 = note["note2"] as? String
let note3 = note["note3"] as? String
let note4 = note["note4"] as? String
let note5 = note["note5"] as? String
let noteinfo = SecPage(note1: note1 ,note2: note2,note3: note3, note4: note4, note5: note5, note6: note6)
noteArray.append(noteinfo) }} } catch{print("note not found")}
return noteArray
}
查看
class CollectionViewCell2: UICollectionViewCell {
@IBOutlet weak var img: UIImageView!
@IBOutlet weak var names: UILabel!
func updateUIsec(data:dataObj) {
names.text = data.note1
}
}
当我只能在name.text
中输入一个数据以及如何有效地使用Model class
检索json
文件并发送到view
时,如何在单元格中更新。
答案 0 :(得分:1)
我仍然不确定这是否是您正在寻找的,您的问题太广泛且难以理解。评论答案,我会根据需要更新答案;)
VC1:
class VC1: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
@IBOutlet weak var collectionView: UICollectionView!
var modelData = [SecPage]()
var selectedItem: SecPage?
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
//collectionView.register... // Register UICollectionView cell subclass
modelData = SecPage.downSec()
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let secPage = modelData[indexPath.row]
let cell = collectionView.dequeue...
cell.textLabel.text = secPage.main
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? VC2{
destination.selectedItem = selectedItem
}
}
override func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
selectedItem = modelData[indexPath.row]
performSegue...
}
}
VC2:
class VC2: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
@IBOutlet weak var collectionView: UICollectionView!
var selectedItem: SecPage? = nil
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
//collectionView.register... // Register UICollectionView cell subclass
modelData = SecPage.downSec()
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeue...
cell.updateUIsec(data: selectedItem!)
return cell
}
}
细胞:
class CollectionViewCell2: UICollectionViewCell {
@IBOutlet weak var img: UIImageView!
@IBOutlet weak var names: UILabel!
func updateUIsec(data: SecPage) {
names.text = data.note1
}
}