填充在集合视图中的图像视图
populate imageview which inside collection view
我正在收到回复,我只想从整个数据中获取图像 URL 并首先填充到集合视图中让我向您展示我的代码
代码
func callSubChapAPI(){
let preferences = UserDefaults.standard
let studentlvl = "student_lvl"
let student_lvl = preferences.object(forKey: studentlvl) as! String
print(student_lvl)
let params = ["level_id": student_lvl]
Alamofire.request(subListWithChapter, method: .post, parameters: params).responseData() { (response) in
switch response.result {
case .success(let data):
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let subjects = try decoder.decode(SubjectResponse.self, from: data)
let urls = subjects.subjectList.map({ [=10=].subList.map({ [=10=].chImage}) }).reduce([], +)
print(urls)
print(subjects)
} catch {
print(error.localizedDescription)
}
case .failure(let error):
print(error.localizedDescription)
}
}
}
这是我的结构
struct SubjectResponse: Decodable {
let subjectList: [Subject]
}
struct Subject: Decodable {
let subList: [Chapter]
}
struct Chapter: Decodable {
let chId : String
let chImage: String
let chName: String
let conId: String
let levelId: String
let subId: String
}
所以现在我想要 chImage URL 并设置为 collectionview
这是回复
{
"subject_list" = (
{
"con_id" = 2;
"level_id" = 1;
"sub_id" = 4;
"sub_list" = (
{
"ch_id" = 17;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1530600693.jpg";
"ch_name" = " 01. Measurement";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 4;
},
{
"ch_id" = 23;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1451930609.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 4;
},
{
"ch_id" = 24;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1884777188.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 4;
},
{
"ch_id" = 25;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1518702048.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 4;
}
);
"sub_name" = Physics;
},
{
"con_id" = 2;
"level_id" = 1;
"sub_id" = 8;
"sub_list" = (
{
"ch_id" = 26;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1437196139.jpg";
"ch_name" = " 1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 8;
},
{
"ch_id" = 27;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1903171865.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 8;
}
);
"sub_name" = Chemistry;
},
{
"con_id" = 2;
"level_id" = 1;
"sub_id" = 9;
"sub_list" = (
{
"ch_id" = 31;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1319333294.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 9;
}
);
"sub_name" = Testing;
},
{
"con_id" = 2;
"level_id" = 1;
"sub_id" = 10;
"sub_list" = (
{
"ch_id" = 28;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1373218664.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 10;
}
);
"sub_name" = "Test Subject";
},
{
"con_id" = 2;
"level_id" = 1;
"sub_id" = 11;
"sub_list" = (
{
"ch_id" = 29;
"ch_image" = "http://mobileapp.xmeducation.com/upload/246189282.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 11;
}
);
"sub_name" = "Test Subject 1";
},
{
"con_id" = 2;
"level_id" = 1;
"sub_id" = 12;
"sub_list" = (
{
"ch_id" = 30;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1342731807.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 12;
}
);
"sub_name" = "Test Subject 2";
}
);
所以请告诉我如何得到 ch_image URL 所以,我只想知道从 SubjectResponse 我怎样才能得到 ch_image URL
请查找以下代码。将 numberOfSections
计数设置为 subjectList
的计数。并将每个部分的 numberOfItemsInSection
设置为 subjectList[section]
的计数。最后在 cellForItemAt
方法中获取图像 url 并在图像上设置。
func numberOfSections(in collectionView: UICollectionView) -> Int {
return subjectList.count
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return subjectList[section].count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// Dequeue your cell here and set the image from
// let image_url = subjectList[IndexPath.section][IndexPath.row].ch_image
}
用于将数据导入 subjectList
。在 top
上定义 subjectList
变量
var subjectList : [Subject]!
在您的 Alamofire 成功案例中:
case .success(let data):
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let subjects = try decoder.decode(SubjectResponse.self, from: data)
self.subjectList = subjects.subjectList
collectionView.reloadData()
所以我的答案是:
let subjects: SubjectResponse?
然后:
self.subjects = try decoder.decode(SubjectResponse.self, from: data)
func numberOfSections(in collectionView: UICollectionView) -> Int {
return self.subjects.subjectList.count
}
func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
return self.subjects.subjectList[section].count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt
indexPath: IndexPath) -> UICollectionViewCell {
var url = subject.subjectList[IndexPath.section].sublist[IndexPath.row].chImage
}
我正在收到回复,我只想从整个数据中获取图像 URL 并首先填充到集合视图中让我向您展示我的代码
代码
func callSubChapAPI(){
let preferences = UserDefaults.standard
let studentlvl = "student_lvl"
let student_lvl = preferences.object(forKey: studentlvl) as! String
print(student_lvl)
let params = ["level_id": student_lvl]
Alamofire.request(subListWithChapter, method: .post, parameters: params).responseData() { (response) in
switch response.result {
case .success(let data):
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let subjects = try decoder.decode(SubjectResponse.self, from: data)
let urls = subjects.subjectList.map({ [=10=].subList.map({ [=10=].chImage}) }).reduce([], +)
print(urls)
print(subjects)
} catch {
print(error.localizedDescription)
}
case .failure(let error):
print(error.localizedDescription)
}
}
}
这是我的结构
struct SubjectResponse: Decodable {
let subjectList: [Subject]
}
struct Subject: Decodable {
let subList: [Chapter]
}
struct Chapter: Decodable {
let chId : String
let chImage: String
let chName: String
let conId: String
let levelId: String
let subId: String
}
所以现在我想要 chImage URL 并设置为 collectionview
这是回复
{
"subject_list" = (
{
"con_id" = 2;
"level_id" = 1;
"sub_id" = 4;
"sub_list" = (
{
"ch_id" = 17;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1530600693.jpg";
"ch_name" = " 01. Measurement";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 4;
},
{
"ch_id" = 23;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1451930609.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 4;
},
{
"ch_id" = 24;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1884777188.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 4;
},
{
"ch_id" = 25;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1518702048.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 4;
}
);
"sub_name" = Physics;
},
{
"con_id" = 2;
"level_id" = 1;
"sub_id" = 8;
"sub_list" = (
{
"ch_id" = 26;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1437196139.jpg";
"ch_name" = " 1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 8;
},
{
"ch_id" = 27;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1903171865.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 8;
}
);
"sub_name" = Chemistry;
},
{
"con_id" = 2;
"level_id" = 1;
"sub_id" = 9;
"sub_list" = (
{
"ch_id" = 31;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1319333294.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 9;
}
);
"sub_name" = Testing;
},
{
"con_id" = 2;
"level_id" = 1;
"sub_id" = 10;
"sub_list" = (
{
"ch_id" = 28;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1373218664.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 10;
}
);
"sub_name" = "Test Subject";
},
{
"con_id" = 2;
"level_id" = 1;
"sub_id" = 11;
"sub_list" = (
{
"ch_id" = 29;
"ch_image" = "http://mobileapp.xmeducation.com/upload/246189282.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 11;
}
);
"sub_name" = "Test Subject 1";
},
{
"con_id" = 2;
"level_id" = 1;
"sub_id" = 12;
"sub_list" = (
{
"ch_id" = 30;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1342731807.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 12;
}
);
"sub_name" = "Test Subject 2";
}
);
所以请告诉我如何得到 ch_image URL 所以,我只想知道从 SubjectResponse 我怎样才能得到 ch_image URL
请查找以下代码。将 numberOfSections
计数设置为 subjectList
的计数。并将每个部分的 numberOfItemsInSection
设置为 subjectList[section]
的计数。最后在 cellForItemAt
方法中获取图像 url 并在图像上设置。
func numberOfSections(in collectionView: UICollectionView) -> Int {
return subjectList.count
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return subjectList[section].count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// Dequeue your cell here and set the image from
// let image_url = subjectList[IndexPath.section][IndexPath.row].ch_image
}
用于将数据导入 subjectList
。在 top
subjectList
变量
var subjectList : [Subject]!
在您的 Alamofire 成功案例中:
case .success(let data):
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let subjects = try decoder.decode(SubjectResponse.self, from: data)
self.subjectList = subjects.subjectList
collectionView.reloadData()
所以我的答案是:
let subjects: SubjectResponse?
然后:
self.subjects = try decoder.decode(SubjectResponse.self, from: data)
func numberOfSections(in collectionView: UICollectionView) -> Int {
return self.subjects.subjectList.count
}
func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
return self.subjects.subjectList[section].count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt
indexPath: IndexPath) -> UICollectionViewCell {
var url = subject.subjectList[IndexPath.section].sublist[IndexPath.row].chImage
}