Reputation: 613
I am creating horizontal collection view and collection view is inside the tableview cell everything works perfect in tableview but in collection view I can not able to display section wise let me show you my response
Response
{
"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";
}
);
I want to display sub_list images section wise but not show as per section let me show my code
Code
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
}
}
I want section wise images I hope you will understand what I mean to tell you and in any confusion then please ask in comment and please tell me what will be the solution for issue
Please check viewcontroller code
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)
}
})
}
tableview methods
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
}
Upvotes: 1
Views: 119
Reputation: 9503
1. Your UI hierarchy should be like this below image.
In MainController
, your tableView cells depends on your number of subjects (phy, chem, math etc)
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return subject_List.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! ExploreTableViewCell
cell.lblTitle.text = subject_List[indexPath.row]. sub_name
cell.arrSubList = subject_List.sub_list
return cell
}
In ExploreTableViewCell
class ExploreTableViewCell : UICollectionViewDataSource,UICollectionViewDelegate {
var arrSubList = [sub_list]()
func numberOfSections(in collectionView: UICollectionView) -> Int {
return self.arrSubList.count
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//let section1 = subjects?.subjectList[section]
return arrSubList.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! ExploreCollectionViewCell
let aSubList = arrSubList[indexPath.row]
let imageUrl = URL(string: aSubList.ch_image)!
cell.imageView.sd_setImage(with: imageUrl, placeholderImage: UIImage(named: "logo_grey"), options: .refreshCached, completed: nil)
cell.lblChapName.text = aSubList.ch_name
return cell
}
}
Note :
May be you get datatype error or some other, just because I does not have proper model name and conventions that you are using.
From above you got basic idea about how maintain the complex structure easily in MVC. For any query you can further ask.
Edit
Upvotes: 1