Я создаю горизонтальное представление коллекции, и представление коллекции находится внутри ячейки табличного представления, все отлично работает в представлении таблицы, но в представлении коллекции я не могу отобразить сечениея покажу вам мой ответ
Ответ
{
"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";
}
);
Я хочу отображать под-списки изображений мудро, но не показывать в соответствии с разделом, позвольте мне показать мой код
Код
var urls = [String]()
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
}
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
// self.subjects = try decoder.decode(SubjectResponse.self, from: data)
let subjects = try decoder.decode(SubjectResponse.self, from: data); print(subjects)
var indexPath: IndexPath?
let url = subjects.subjectList[indexPath?.section ?? 0].subList[indexPath?.row ?? 0].chImage
print(url)
self.urls.append(url)
self.collView.reloadData()
} catch {
print(error.localizedDescription)
}
case .failure(let error):
print(error.localizedDescription)
}
}
}
}
extension ExploreTableViewCell : UICollectionViewDataSource,UICollectionViewDelegate {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return self.urls.count
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//let section1 = subjects?.subjectList[section]
return urls[section].count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! ExploreCollectionViewCell
let imageUrl = URL(string: urls[indexPath.row])!
cell.imageView.sd_setImage(with: imageUrl, placeholderImage: UIImage(named: "logo_grey"), options: .refreshCached, completed: nil)
//let name = subjects?.subjectList[indexPath.section].subList[indexPath.row].chName
//cell.lblChapName.text = name
return cell
}
}
Я хочу раздел мудрых изображений Я надеюсь, вы поймете, что я имею в виду, чтобы сказать вам, и в любой путанице, пожалуйста, спросите в комментарии и, пожалуйста, скажите мне, что будетрешение проблемы
Пожалуйста, проверьте код контроллера вида
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).responseJSON(completionHandler: {(response) in
switch response.result{
case.success(let value):
let result = response.result
if let dict = result.value as? Dictionary<String,AnyObject>{
if let categorylist = dict["subject_list"]{
self.subList = categorylist as! [AnyObject]
print(self.subList)
self.tbllist.reloadData()
}
}
case.failure(let error):
print(error.localizedDescription)
}
})
}
методы просмотра таблицы
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return subList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! ExploreTableViewCell
let name = subList[indexPath.row]["sub_name"]
cell.lblTitle.text = name as? String
return cell
}