How to return dynamic numberOfItemsInSection in UICollectionview
I have viewForHeaderInSection
in UITableView
based on viewForHeaderInSection
data each UITableviewCell
have UICollectionView
in UICollectionViewCell
I have to show data for "product" and "qty" the count of each "product" and "qty" is different for each viewForHeaderInSection
. I am able to achieve tableview header and cell data but I am not able to get UICollectionViewCell
data based on UITableViewHeader
data in the json "order_id", "order_unique_id","store_name" and "otp_store" is tableview header section data and "user_details" is tableviewcell
data for uicollectionviewcell
"product" and "qty"
{
"status": "1",
"error": false,
"data": [
{
"order_id": "11",
"order_unique_id": "ORDR-1001",
"store_name": "24X7",
"otp_store": "781103",
"product": [
"Product One",
"Product Two"
],
"qty": [
"1",
"3"
],
"user_details": {
"name": "Pankaj Ravi",
"number": "0999889",
"landmark": "PBH",
"area": "Bow East",
"pincode": "E3 9EG",
"place": "Home"
},
"status": "2",
"date": "2018-12-13",
"time": "14:37:57"
},
{
"order_id": "1",
"order_unique_id": "ORDR-988",
"store_name": "24X7",
"otp_store": "957505",
"product": [
"Product Eight"
],
"qty": [
"1"
],
"user_details": {
"name": “Jhon”,
"number": “999996",
"landmark": “Ithum",
"area": "Bow East",
"pincode": "E3 9EG",
"place": "Office"
},
"status": "0",
"date": "2018-11-24",
"time": "12:41:02"
}
]
}
Code For UITableview
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
print(section)
let headerCell = Bundle.main.loadNibNamed("CustomHeaderCell", owner: self, options: nil)?.first as! CustomHeaderCell
if runnerArray.count > 0 {
headerCell.btnHeader.setTitle("("x " + runnerArray[section].order_unique_id)", for: UIControl.State.normal)
var status = runnerArray[section].status
if status == "0" {
status = "Pending"
} else if status == "1" {
status = "Dispatch"
} else {
status = "Complete"
}
let lblHeaderText = status + " " + runnerArray[section].time + " , " + runnerArray[section].date
headerCell.lblHeader.text = lblHeaderText
return headerCell
} else {
return UIView()
}
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}
func numberOfSections(in tableView: UITableView) -> Int {
return runnerArray.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 400
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as? CustomTableViewCell {
cell.setDataForOTPStoreAndName(runner: runnerArray[indexPath.section])
cell.setDataForProduct(userDetails: userDetailsArray[indexPath.section])
cell.selectionStyle = .none
return cell
}
return UITableViewCell()
}
Code For UICollectionView Cell
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//Note:- Here i am not able to find how to return array count based on UItableviewheadersection data
return productArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ProductsCollectionViewCell", for: indexPath) as? ProductsCollectionViewCell {
cell.lblProductName.text = productArray[indexPath.row]
return cell
}
return UICollectionViewCell()
}
uicollectionview swift4 uicollectionviewcell uitableviewsectionheader
add a comment |
I have viewForHeaderInSection
in UITableView
based on viewForHeaderInSection
data each UITableviewCell
have UICollectionView
in UICollectionViewCell
I have to show data for "product" and "qty" the count of each "product" and "qty" is different for each viewForHeaderInSection
. I am able to achieve tableview header and cell data but I am not able to get UICollectionViewCell
data based on UITableViewHeader
data in the json "order_id", "order_unique_id","store_name" and "otp_store" is tableview header section data and "user_details" is tableviewcell
data for uicollectionviewcell
"product" and "qty"
{
"status": "1",
"error": false,
"data": [
{
"order_id": "11",
"order_unique_id": "ORDR-1001",
"store_name": "24X7",
"otp_store": "781103",
"product": [
"Product One",
"Product Two"
],
"qty": [
"1",
"3"
],
"user_details": {
"name": "Pankaj Ravi",
"number": "0999889",
"landmark": "PBH",
"area": "Bow East",
"pincode": "E3 9EG",
"place": "Home"
},
"status": "2",
"date": "2018-12-13",
"time": "14:37:57"
},
{
"order_id": "1",
"order_unique_id": "ORDR-988",
"store_name": "24X7",
"otp_store": "957505",
"product": [
"Product Eight"
],
"qty": [
"1"
],
"user_details": {
"name": “Jhon”,
"number": “999996",
"landmark": “Ithum",
"area": "Bow East",
"pincode": "E3 9EG",
"place": "Office"
},
"status": "0",
"date": "2018-11-24",
"time": "12:41:02"
}
]
}
Code For UITableview
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
print(section)
let headerCell = Bundle.main.loadNibNamed("CustomHeaderCell", owner: self, options: nil)?.first as! CustomHeaderCell
if runnerArray.count > 0 {
headerCell.btnHeader.setTitle("("x " + runnerArray[section].order_unique_id)", for: UIControl.State.normal)
var status = runnerArray[section].status
if status == "0" {
status = "Pending"
} else if status == "1" {
status = "Dispatch"
} else {
status = "Complete"
}
let lblHeaderText = status + " " + runnerArray[section].time + " , " + runnerArray[section].date
headerCell.lblHeader.text = lblHeaderText
return headerCell
} else {
return UIView()
}
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}
func numberOfSections(in tableView: UITableView) -> Int {
return runnerArray.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 400
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as? CustomTableViewCell {
cell.setDataForOTPStoreAndName(runner: runnerArray[indexPath.section])
cell.setDataForProduct(userDetails: userDetailsArray[indexPath.section])
cell.selectionStyle = .none
return cell
}
return UITableViewCell()
}
Code For UICollectionView Cell
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//Note:- Here i am not able to find how to return array count based on UItableviewheadersection data
return productArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ProductsCollectionViewCell", for: indexPath) as? ProductsCollectionViewCell {
cell.lblProductName.text = productArray[indexPath.row]
return cell
}
return UICollectionViewCell()
}
uicollectionview swift4 uicollectionviewcell uitableviewsectionheader
plz past your full code oftableview
andcollectionview
delegate and datasource method
– Pratik Prajapati
Dec 31 '18 at 6:02
@PratikPrajapati added code of UITableview and UICollectionview
– Sanjay Mishra
Dec 31 '18 at 6:11
In tableViewcellForRowAt
method you have to pass your collection data, likecell.productArray = runnerArray[indexPath.section].product
andcell.productArray = runnerArray[indexPath.section].qty
then reload your collectionview incell.collectionView.reloadData()
– Pratik Prajapati
Dec 31 '18 at 6:25
@PratikPrajapati Thanks a lot it works for me
– Sanjay Mishra
Dec 31 '18 at 10:20
add a comment |
I have viewForHeaderInSection
in UITableView
based on viewForHeaderInSection
data each UITableviewCell
have UICollectionView
in UICollectionViewCell
I have to show data for "product" and "qty" the count of each "product" and "qty" is different for each viewForHeaderInSection
. I am able to achieve tableview header and cell data but I am not able to get UICollectionViewCell
data based on UITableViewHeader
data in the json "order_id", "order_unique_id","store_name" and "otp_store" is tableview header section data and "user_details" is tableviewcell
data for uicollectionviewcell
"product" and "qty"
{
"status": "1",
"error": false,
"data": [
{
"order_id": "11",
"order_unique_id": "ORDR-1001",
"store_name": "24X7",
"otp_store": "781103",
"product": [
"Product One",
"Product Two"
],
"qty": [
"1",
"3"
],
"user_details": {
"name": "Pankaj Ravi",
"number": "0999889",
"landmark": "PBH",
"area": "Bow East",
"pincode": "E3 9EG",
"place": "Home"
},
"status": "2",
"date": "2018-12-13",
"time": "14:37:57"
},
{
"order_id": "1",
"order_unique_id": "ORDR-988",
"store_name": "24X7",
"otp_store": "957505",
"product": [
"Product Eight"
],
"qty": [
"1"
],
"user_details": {
"name": “Jhon”,
"number": “999996",
"landmark": “Ithum",
"area": "Bow East",
"pincode": "E3 9EG",
"place": "Office"
},
"status": "0",
"date": "2018-11-24",
"time": "12:41:02"
}
]
}
Code For UITableview
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
print(section)
let headerCell = Bundle.main.loadNibNamed("CustomHeaderCell", owner: self, options: nil)?.first as! CustomHeaderCell
if runnerArray.count > 0 {
headerCell.btnHeader.setTitle("("x " + runnerArray[section].order_unique_id)", for: UIControl.State.normal)
var status = runnerArray[section].status
if status == "0" {
status = "Pending"
} else if status == "1" {
status = "Dispatch"
} else {
status = "Complete"
}
let lblHeaderText = status + " " + runnerArray[section].time + " , " + runnerArray[section].date
headerCell.lblHeader.text = lblHeaderText
return headerCell
} else {
return UIView()
}
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}
func numberOfSections(in tableView: UITableView) -> Int {
return runnerArray.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 400
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as? CustomTableViewCell {
cell.setDataForOTPStoreAndName(runner: runnerArray[indexPath.section])
cell.setDataForProduct(userDetails: userDetailsArray[indexPath.section])
cell.selectionStyle = .none
return cell
}
return UITableViewCell()
}
Code For UICollectionView Cell
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//Note:- Here i am not able to find how to return array count based on UItableviewheadersection data
return productArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ProductsCollectionViewCell", for: indexPath) as? ProductsCollectionViewCell {
cell.lblProductName.text = productArray[indexPath.row]
return cell
}
return UICollectionViewCell()
}
uicollectionview swift4 uicollectionviewcell uitableviewsectionheader
I have viewForHeaderInSection
in UITableView
based on viewForHeaderInSection
data each UITableviewCell
have UICollectionView
in UICollectionViewCell
I have to show data for "product" and "qty" the count of each "product" and "qty" is different for each viewForHeaderInSection
. I am able to achieve tableview header and cell data but I am not able to get UICollectionViewCell
data based on UITableViewHeader
data in the json "order_id", "order_unique_id","store_name" and "otp_store" is tableview header section data and "user_details" is tableviewcell
data for uicollectionviewcell
"product" and "qty"
{
"status": "1",
"error": false,
"data": [
{
"order_id": "11",
"order_unique_id": "ORDR-1001",
"store_name": "24X7",
"otp_store": "781103",
"product": [
"Product One",
"Product Two"
],
"qty": [
"1",
"3"
],
"user_details": {
"name": "Pankaj Ravi",
"number": "0999889",
"landmark": "PBH",
"area": "Bow East",
"pincode": "E3 9EG",
"place": "Home"
},
"status": "2",
"date": "2018-12-13",
"time": "14:37:57"
},
{
"order_id": "1",
"order_unique_id": "ORDR-988",
"store_name": "24X7",
"otp_store": "957505",
"product": [
"Product Eight"
],
"qty": [
"1"
],
"user_details": {
"name": “Jhon”,
"number": “999996",
"landmark": “Ithum",
"area": "Bow East",
"pincode": "E3 9EG",
"place": "Office"
},
"status": "0",
"date": "2018-11-24",
"time": "12:41:02"
}
]
}
Code For UITableview
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
print(section)
let headerCell = Bundle.main.loadNibNamed("CustomHeaderCell", owner: self, options: nil)?.first as! CustomHeaderCell
if runnerArray.count > 0 {
headerCell.btnHeader.setTitle("("x " + runnerArray[section].order_unique_id)", for: UIControl.State.normal)
var status = runnerArray[section].status
if status == "0" {
status = "Pending"
} else if status == "1" {
status = "Dispatch"
} else {
status = "Complete"
}
let lblHeaderText = status + " " + runnerArray[section].time + " , " + runnerArray[section].date
headerCell.lblHeader.text = lblHeaderText
return headerCell
} else {
return UIView()
}
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}
func numberOfSections(in tableView: UITableView) -> Int {
return runnerArray.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 400
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as? CustomTableViewCell {
cell.setDataForOTPStoreAndName(runner: runnerArray[indexPath.section])
cell.setDataForProduct(userDetails: userDetailsArray[indexPath.section])
cell.selectionStyle = .none
return cell
}
return UITableViewCell()
}
Code For UICollectionView Cell
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//Note:- Here i am not able to find how to return array count based on UItableviewheadersection data
return productArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ProductsCollectionViewCell", for: indexPath) as? ProductsCollectionViewCell {
cell.lblProductName.text = productArray[indexPath.row]
return cell
}
return UICollectionViewCell()
}
uicollectionview swift4 uicollectionviewcell uitableviewsectionheader
uicollectionview swift4 uicollectionviewcell uitableviewsectionheader
edited Dec 31 '18 at 6:14
Sanjay Mishra
asked Dec 31 '18 at 5:56


Sanjay MishraSanjay Mishra
278
278
plz past your full code oftableview
andcollectionview
delegate and datasource method
– Pratik Prajapati
Dec 31 '18 at 6:02
@PratikPrajapati added code of UITableview and UICollectionview
– Sanjay Mishra
Dec 31 '18 at 6:11
In tableViewcellForRowAt
method you have to pass your collection data, likecell.productArray = runnerArray[indexPath.section].product
andcell.productArray = runnerArray[indexPath.section].qty
then reload your collectionview incell.collectionView.reloadData()
– Pratik Prajapati
Dec 31 '18 at 6:25
@PratikPrajapati Thanks a lot it works for me
– Sanjay Mishra
Dec 31 '18 at 10:20
add a comment |
plz past your full code oftableview
andcollectionview
delegate and datasource method
– Pratik Prajapati
Dec 31 '18 at 6:02
@PratikPrajapati added code of UITableview and UICollectionview
– Sanjay Mishra
Dec 31 '18 at 6:11
In tableViewcellForRowAt
method you have to pass your collection data, likecell.productArray = runnerArray[indexPath.section].product
andcell.productArray = runnerArray[indexPath.section].qty
then reload your collectionview incell.collectionView.reloadData()
– Pratik Prajapati
Dec 31 '18 at 6:25
@PratikPrajapati Thanks a lot it works for me
– Sanjay Mishra
Dec 31 '18 at 10:20
plz past your full code of
tableview
and collectionview
delegate and datasource method– Pratik Prajapati
Dec 31 '18 at 6:02
plz past your full code of
tableview
and collectionview
delegate and datasource method– Pratik Prajapati
Dec 31 '18 at 6:02
@PratikPrajapati added code of UITableview and UICollectionview
– Sanjay Mishra
Dec 31 '18 at 6:11
@PratikPrajapati added code of UITableview and UICollectionview
– Sanjay Mishra
Dec 31 '18 at 6:11
In tableView
cellForRowAt
method you have to pass your collection data, like cell.productArray = runnerArray[indexPath.section].product
and cell.productArray = runnerArray[indexPath.section].qty
then reload your collectionview in cell.collectionView.reloadData()
– Pratik Prajapati
Dec 31 '18 at 6:25
In tableView
cellForRowAt
method you have to pass your collection data, like cell.productArray = runnerArray[indexPath.section].product
and cell.productArray = runnerArray[indexPath.section].qty
then reload your collectionview in cell.collectionView.reloadData()
– Pratik Prajapati
Dec 31 '18 at 6:25
@PratikPrajapati Thanks a lot it works for me
– Sanjay Mishra
Dec 31 '18 at 10:20
@PratikPrajapati Thanks a lot it works for me
– Sanjay Mishra
Dec 31 '18 at 10:20
add a comment |
1 Answer
1
active
oldest
votes
you add this code to show number of header and each header add one row,then pass one section object to table cell use below code.
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
var dataArry : [DataObj]?
override func viewDidLoad() {
super.viewDidLoad()
if let path = Bundle.main.path(forResource: "response", ofType: "json") {
do {
let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .alwaysMapped)
let jsonObj = try JSONDecoder().decode(Modal.self, from: data)
self.dataArry = jsonObj.data
print("jsonData:(jsonObj)")
} catch let error {
print("parse error: (error.localizedDescription)")
}
} else {
print("Invalid filename/path.")
}
}
func numberOfSections(in tableView: UITableView) -> Int {
if let count = self.dataArry?.count {
return count
}
return 0
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "RowCell") as? RowCell
cell?.updateCell(dataObj: self.dataArry?[indexPath.section] ?? DataObj())
return cell!
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell") as? HeaderCell
cell?.textLabel?.text = dataArry?[section].store_name
return cell
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 60
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 200
}
}
then add this code in your tableview cell class
func updateCell(dataObj:DataObj) {
self.dataObj = dataObj
collectionView.delegate = self
collectionView.dataSource = self
collectionView.reloadData()
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == 0 {
if let count = self.dataObj?.product?.count {
return count
}
}else if section == 1 {
if let count = self.dataObj?.qty?.count {
return count
}
}
return 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.section == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as? CollectionViewCell
cell?.titlelbl.text = self.dataObj?.product?[indexPath.row]
return cell!
}else if indexPath.section == 1 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as?
CollectionViewCell
//cell?.backgroundColor = UIColor.yellow
cell?.titlelbl.text = self.dataObj?.qty?[indexPath.row]
return cell!
}
return UICollectionViewCell()
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 100.0, height: 100.0)
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53984070%2fhow-to-return-dynamic-numberofitemsinsection-in-uicollectionview%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
you add this code to show number of header and each header add one row,then pass one section object to table cell use below code.
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
var dataArry : [DataObj]?
override func viewDidLoad() {
super.viewDidLoad()
if let path = Bundle.main.path(forResource: "response", ofType: "json") {
do {
let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .alwaysMapped)
let jsonObj = try JSONDecoder().decode(Modal.self, from: data)
self.dataArry = jsonObj.data
print("jsonData:(jsonObj)")
} catch let error {
print("parse error: (error.localizedDescription)")
}
} else {
print("Invalid filename/path.")
}
}
func numberOfSections(in tableView: UITableView) -> Int {
if let count = self.dataArry?.count {
return count
}
return 0
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "RowCell") as? RowCell
cell?.updateCell(dataObj: self.dataArry?[indexPath.section] ?? DataObj())
return cell!
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell") as? HeaderCell
cell?.textLabel?.text = dataArry?[section].store_name
return cell
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 60
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 200
}
}
then add this code in your tableview cell class
func updateCell(dataObj:DataObj) {
self.dataObj = dataObj
collectionView.delegate = self
collectionView.dataSource = self
collectionView.reloadData()
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == 0 {
if let count = self.dataObj?.product?.count {
return count
}
}else if section == 1 {
if let count = self.dataObj?.qty?.count {
return count
}
}
return 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.section == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as? CollectionViewCell
cell?.titlelbl.text = self.dataObj?.product?[indexPath.row]
return cell!
}else if indexPath.section == 1 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as?
CollectionViewCell
//cell?.backgroundColor = UIColor.yellow
cell?.titlelbl.text = self.dataObj?.qty?[indexPath.row]
return cell!
}
return UICollectionViewCell()
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 100.0, height: 100.0)
}
}
add a comment |
you add this code to show number of header and each header add one row,then pass one section object to table cell use below code.
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
var dataArry : [DataObj]?
override func viewDidLoad() {
super.viewDidLoad()
if let path = Bundle.main.path(forResource: "response", ofType: "json") {
do {
let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .alwaysMapped)
let jsonObj = try JSONDecoder().decode(Modal.self, from: data)
self.dataArry = jsonObj.data
print("jsonData:(jsonObj)")
} catch let error {
print("parse error: (error.localizedDescription)")
}
} else {
print("Invalid filename/path.")
}
}
func numberOfSections(in tableView: UITableView) -> Int {
if let count = self.dataArry?.count {
return count
}
return 0
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "RowCell") as? RowCell
cell?.updateCell(dataObj: self.dataArry?[indexPath.section] ?? DataObj())
return cell!
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell") as? HeaderCell
cell?.textLabel?.text = dataArry?[section].store_name
return cell
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 60
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 200
}
}
then add this code in your tableview cell class
func updateCell(dataObj:DataObj) {
self.dataObj = dataObj
collectionView.delegate = self
collectionView.dataSource = self
collectionView.reloadData()
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == 0 {
if let count = self.dataObj?.product?.count {
return count
}
}else if section == 1 {
if let count = self.dataObj?.qty?.count {
return count
}
}
return 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.section == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as? CollectionViewCell
cell?.titlelbl.text = self.dataObj?.product?[indexPath.row]
return cell!
}else if indexPath.section == 1 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as?
CollectionViewCell
//cell?.backgroundColor = UIColor.yellow
cell?.titlelbl.text = self.dataObj?.qty?[indexPath.row]
return cell!
}
return UICollectionViewCell()
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 100.0, height: 100.0)
}
}
add a comment |
you add this code to show number of header and each header add one row,then pass one section object to table cell use below code.
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
var dataArry : [DataObj]?
override func viewDidLoad() {
super.viewDidLoad()
if let path = Bundle.main.path(forResource: "response", ofType: "json") {
do {
let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .alwaysMapped)
let jsonObj = try JSONDecoder().decode(Modal.self, from: data)
self.dataArry = jsonObj.data
print("jsonData:(jsonObj)")
} catch let error {
print("parse error: (error.localizedDescription)")
}
} else {
print("Invalid filename/path.")
}
}
func numberOfSections(in tableView: UITableView) -> Int {
if let count = self.dataArry?.count {
return count
}
return 0
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "RowCell") as? RowCell
cell?.updateCell(dataObj: self.dataArry?[indexPath.section] ?? DataObj())
return cell!
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell") as? HeaderCell
cell?.textLabel?.text = dataArry?[section].store_name
return cell
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 60
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 200
}
}
then add this code in your tableview cell class
func updateCell(dataObj:DataObj) {
self.dataObj = dataObj
collectionView.delegate = self
collectionView.dataSource = self
collectionView.reloadData()
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == 0 {
if let count = self.dataObj?.product?.count {
return count
}
}else if section == 1 {
if let count = self.dataObj?.qty?.count {
return count
}
}
return 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.section == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as? CollectionViewCell
cell?.titlelbl.text = self.dataObj?.product?[indexPath.row]
return cell!
}else if indexPath.section == 1 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as?
CollectionViewCell
//cell?.backgroundColor = UIColor.yellow
cell?.titlelbl.text = self.dataObj?.qty?[indexPath.row]
return cell!
}
return UICollectionViewCell()
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 100.0, height: 100.0)
}
}
you add this code to show number of header and each header add one row,then pass one section object to table cell use below code.
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
var dataArry : [DataObj]?
override func viewDidLoad() {
super.viewDidLoad()
if let path = Bundle.main.path(forResource: "response", ofType: "json") {
do {
let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .alwaysMapped)
let jsonObj = try JSONDecoder().decode(Modal.self, from: data)
self.dataArry = jsonObj.data
print("jsonData:(jsonObj)")
} catch let error {
print("parse error: (error.localizedDescription)")
}
} else {
print("Invalid filename/path.")
}
}
func numberOfSections(in tableView: UITableView) -> Int {
if let count = self.dataArry?.count {
return count
}
return 0
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "RowCell") as? RowCell
cell?.updateCell(dataObj: self.dataArry?[indexPath.section] ?? DataObj())
return cell!
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell") as? HeaderCell
cell?.textLabel?.text = dataArry?[section].store_name
return cell
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 60
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 200
}
}
then add this code in your tableview cell class
func updateCell(dataObj:DataObj) {
self.dataObj = dataObj
collectionView.delegate = self
collectionView.dataSource = self
collectionView.reloadData()
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == 0 {
if let count = self.dataObj?.product?.count {
return count
}
}else if section == 1 {
if let count = self.dataObj?.qty?.count {
return count
}
}
return 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.section == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as? CollectionViewCell
cell?.titlelbl.text = self.dataObj?.product?[indexPath.row]
return cell!
}else if indexPath.section == 1 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as?
CollectionViewCell
//cell?.backgroundColor = UIColor.yellow
cell?.titlelbl.text = self.dataObj?.qty?[indexPath.row]
return cell!
}
return UICollectionViewCell()
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 100.0, height: 100.0)
}
}
answered Jan 2 at 9:53
sachin datarkarsachin datarkar
414
414
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53984070%2fhow-to-return-dynamic-numberofitemsinsection-in-uicollectionview%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
plz past your full code of
tableview
andcollectionview
delegate and datasource method– Pratik Prajapati
Dec 31 '18 at 6:02
@PratikPrajapati added code of UITableview and UICollectionview
– Sanjay Mishra
Dec 31 '18 at 6:11
In tableView
cellForRowAt
method you have to pass your collection data, likecell.productArray = runnerArray[indexPath.section].product
andcell.productArray = runnerArray[indexPath.section].qty
then reload your collectionview incell.collectionView.reloadData()
– Pratik Prajapati
Dec 31 '18 at 6:25
@PratikPrajapati Thanks a lot it works for me
– Sanjay Mishra
Dec 31 '18 at 10:20