填充集合视图内的图像视图
Posted
技术标签:
【中文标题】填充集合视图内的图像视图【英文标题】:populate imageview which inside collection view 【发布时间】:2018-12-18 07:24:59 【问题描述】:我收到了回复,我只想从整个数据中获取图像 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( $0.subList.map( $0.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 所以,我只想知道 From SubjectResponse 如何获取 ch_image URL
【问题讨论】:
而不是响应json
,尝试添加UICollectionView
相关代码。解析响应后也调用DispatchQueue.main.async self.collectionView.reloadData()
。
你是要解码还是出错?
我正在获取所有图片网址,但我不明白如何在收藏视图中填充
请检查我更新了我正在获取图像 url 的代码,但是我如何在无法理解的 collectionview 中填充
【参考方案1】:
请在下面找到代码。将 numberOfSections
计数设置为 subjectList
的计数。并为每个部分设置 numberOfItemsInSection
到 subjectList[section]
的计数。最后在cellForItemAt
方法中获取您的图片网址并在图片上设置。
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
。在顶部定义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()
【讨论】:
你能告诉我你从哪里得到的subjectList 请告诉我你从哪里可以得到 subjectLict 数组 只需将您的 json 响应映射到 subjectList 并重新加载 collectionView【参考方案2】:所以我的答案是:
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
【讨论】:
我仍然无法获得 subjectList 我有你提供的代码 错误是这样的:使用未解析的标识符'subjectList' 你在VC中定义了subjectList数组吗? 我在集合视图方法 cellForItemAt Indexpath 中遇到错误 你能展示你从 JSONDecoder() 得到的数组吗?以上是关于填充集合视图内的图像视图的主要内容,如果未能解决你的问题,请参考以下文章