nested sections in the tableview in swift











up vote
0
down vote

favorite












{
"data":[
{

"mainquestion":"was the staff",
"items":[

{
"button_type":"2",
"question": "Gender",
"options": ["Male","Female"]

},
{
"button_type":"2",
"question": "How old are you",
"options": ["Under 18","Age 18 to 35","Age 36 to 50","Age 51 to 70","70 and above"]

}
]
}, {

"mainquestion":"Please tell the feedback",
"items":[

{
"button_type":"2",
"question": "Gender",
"options": ["Male","Female"]

},
{
"button_type":"2",
"question": "How old are you",
"options": ["Under 18","Age 18 to 35","Age 36 to 50","Age 51 to 70","70 and above"]

},
{
"button_type":"2",
"question": "How old are you",
"options": ["Under 18","Age 18 to 35","Age 36 to 50","Age 51 to 70","70 and above"]

}
]
}
]
}


This is my json data.I need to display the data in the tableview as below screen shot.



This is the screen shot.I need to display in this way.



Currently my code is below.


func numberOfSections(in tableView: UITableView) -> Int {

return dummyDataViewModel.numberOfSections()
print(dummyDataViewModel.numberOfSections())

}


func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

let identifier = "DummyDataHeaderCell"


var headercell: EQ_DummyDataHeader! = tableView.dequeueReusableCell(withIdentifier: identifier) as? EQ_DummyDataHeader

if headercell == nil {

tableView.register(UINib(nibName: "EQ_DummyDataHeader", bundle: nil), forCellReuseIdentifier: identifier)

headercell = tableView.dequeueReusableCell(withIdentifier: identifier) as? EQ_DummyDataHeader

}

headercell.setReviewData(reviews:self.dummyDataViewModel.titleForHeaderInSection(atsection:section))

return headercell

}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

if tableView == tableview{

return 50
}
return 20
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return dummyDataViewModel.numberOfRowsIn(section: section)

}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let model = dummyDataViewModel.titleForHeaderInSection(atsection: indexPath.section)
print(model.answerType1)

print(model.answerType1?.rawValue)

let c = model.answerType1
let cellClass = c?.cellType().getClass()
print(cellClass)
let cell = tableView.dequeueReusableCell(withIdentifier: (cellClass?.cellReuseIdentifier())!, for: indexPath) as! BaseCell
print(cell)


cell.selectionStyle = .none

let optionModel = dummyDataViewModel.datafordisplay(atindex: indexPath)
cell.setOptions(Options1: optionModel)

cell.type = c?.cellType()

cell.delegate = self
cell.textdelegate = self
cell.textdelegate1 = self

cell.datedelegateDatepicker1 = self

cell.selectDateDelegate = self
if optionModel.isSelected! {
cell.setOptions1(OptionsSelected:optionModel)
cell.TextDisplay(options:optionModel)


} else {
cell.setOptions1(OptionsisSelected:optionModel)
cell.setOptions(OptionsisSelected:optionModel)
cell.setOptions1(OptionsisSelected:optionModel)
cell.TextNotDisplay(options:optionModel)

}
print(cell.type)

if cell.type == .starratingtype{

cell.selectionStyle = .none
}
else if cell.type == .smileytype{

cell.selectionStyle = .none
}
else if cell.type == .checkboxtype{
cell.selectionStyle = .none
}
else if cell.type == .radiotype{
cell.selectionStyle = .none
}
else if cell.type == .textfieldtype{
cell.selectionStyle = .none
}

else if cell.type == .smileytype{
cell.selectionStyle = .none
}


return cell


}


In the view model:-



 func numberOfSections() -> Int{
print((datasourceModel.dataListArray?.count)!)
return (datasourceModel.dataListArray?.count)!
}

func titleForHeaderInSection(atsection section: Int) -> EQ_DummyDataModel {
print(datasourceModel.dataListArray![section])
return datasourceModel.dataListArray![section]
}

func numberOfRowsIn(section:Int) -> Int {

print( datasourceModel.dataListArray?[section].optionsModelArray.count ?? 0)
return datasourceModel.dataListArray?[section].optionsModelArray.count ?? 0

}

func datafordisplay(atindex indexPath: IndexPath) -> EQ_OptionModel{

print(datasourceModel.dataListArray![indexPath.section].optionsModelArray[indexPath.row])

return datasourceModel.dataListArray![indexPath.section].optionsModelArray[indexPath.row]
}


I am doing in mvvm .So how to implement the nested section in this code?
What are the changes needed to do in the number of sections and datasource.










share|improve this question
























  • how to implement it?
    – clydececiljohn
    Nov 22 at 10:02










  • How to implement the nested sections in the tableview?
    – clydececiljohn
    Nov 22 at 10:45















up vote
0
down vote

favorite












{
"data":[
{

"mainquestion":"was the staff",
"items":[

{
"button_type":"2",
"question": "Gender",
"options": ["Male","Female"]

},
{
"button_type":"2",
"question": "How old are you",
"options": ["Under 18","Age 18 to 35","Age 36 to 50","Age 51 to 70","70 and above"]

}
]
}, {

"mainquestion":"Please tell the feedback",
"items":[

{
"button_type":"2",
"question": "Gender",
"options": ["Male","Female"]

},
{
"button_type":"2",
"question": "How old are you",
"options": ["Under 18","Age 18 to 35","Age 36 to 50","Age 51 to 70","70 and above"]

},
{
"button_type":"2",
"question": "How old are you",
"options": ["Under 18","Age 18 to 35","Age 36 to 50","Age 51 to 70","70 and above"]

}
]
}
]
}


This is my json data.I need to display the data in the tableview as below screen shot.



This is the screen shot.I need to display in this way.



Currently my code is below.


func numberOfSections(in tableView: UITableView) -> Int {

return dummyDataViewModel.numberOfSections()
print(dummyDataViewModel.numberOfSections())

}


func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

let identifier = "DummyDataHeaderCell"


var headercell: EQ_DummyDataHeader! = tableView.dequeueReusableCell(withIdentifier: identifier) as? EQ_DummyDataHeader

if headercell == nil {

tableView.register(UINib(nibName: "EQ_DummyDataHeader", bundle: nil), forCellReuseIdentifier: identifier)

headercell = tableView.dequeueReusableCell(withIdentifier: identifier) as? EQ_DummyDataHeader

}

headercell.setReviewData(reviews:self.dummyDataViewModel.titleForHeaderInSection(atsection:section))

return headercell

}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

if tableView == tableview{

return 50
}
return 20
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return dummyDataViewModel.numberOfRowsIn(section: section)

}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let model = dummyDataViewModel.titleForHeaderInSection(atsection: indexPath.section)
print(model.answerType1)

print(model.answerType1?.rawValue)

let c = model.answerType1
let cellClass = c?.cellType().getClass()
print(cellClass)
let cell = tableView.dequeueReusableCell(withIdentifier: (cellClass?.cellReuseIdentifier())!, for: indexPath) as! BaseCell
print(cell)


cell.selectionStyle = .none

let optionModel = dummyDataViewModel.datafordisplay(atindex: indexPath)
cell.setOptions(Options1: optionModel)

cell.type = c?.cellType()

cell.delegate = self
cell.textdelegate = self
cell.textdelegate1 = self

cell.datedelegateDatepicker1 = self

cell.selectDateDelegate = self
if optionModel.isSelected! {
cell.setOptions1(OptionsSelected:optionModel)
cell.TextDisplay(options:optionModel)


} else {
cell.setOptions1(OptionsisSelected:optionModel)
cell.setOptions(OptionsisSelected:optionModel)
cell.setOptions1(OptionsisSelected:optionModel)
cell.TextNotDisplay(options:optionModel)

}
print(cell.type)

if cell.type == .starratingtype{

cell.selectionStyle = .none
}
else if cell.type == .smileytype{

cell.selectionStyle = .none
}
else if cell.type == .checkboxtype{
cell.selectionStyle = .none
}
else if cell.type == .radiotype{
cell.selectionStyle = .none
}
else if cell.type == .textfieldtype{
cell.selectionStyle = .none
}

else if cell.type == .smileytype{
cell.selectionStyle = .none
}


return cell


}


In the view model:-



 func numberOfSections() -> Int{
print((datasourceModel.dataListArray?.count)!)
return (datasourceModel.dataListArray?.count)!
}

func titleForHeaderInSection(atsection section: Int) -> EQ_DummyDataModel {
print(datasourceModel.dataListArray![section])
return datasourceModel.dataListArray![section]
}

func numberOfRowsIn(section:Int) -> Int {

print( datasourceModel.dataListArray?[section].optionsModelArray.count ?? 0)
return datasourceModel.dataListArray?[section].optionsModelArray.count ?? 0

}

func datafordisplay(atindex indexPath: IndexPath) -> EQ_OptionModel{

print(datasourceModel.dataListArray![indexPath.section].optionsModelArray[indexPath.row])

return datasourceModel.dataListArray![indexPath.section].optionsModelArray[indexPath.row]
}


I am doing in mvvm .So how to implement the nested section in this code?
What are the changes needed to do in the number of sections and datasource.










share|improve this question
























  • how to implement it?
    – clydececiljohn
    Nov 22 at 10:02










  • How to implement the nested sections in the tableview?
    – clydececiljohn
    Nov 22 at 10:45













up vote
0
down vote

favorite









up vote
0
down vote

favorite











{
"data":[
{

"mainquestion":"was the staff",
"items":[

{
"button_type":"2",
"question": "Gender",
"options": ["Male","Female"]

},
{
"button_type":"2",
"question": "How old are you",
"options": ["Under 18","Age 18 to 35","Age 36 to 50","Age 51 to 70","70 and above"]

}
]
}, {

"mainquestion":"Please tell the feedback",
"items":[

{
"button_type":"2",
"question": "Gender",
"options": ["Male","Female"]

},
{
"button_type":"2",
"question": "How old are you",
"options": ["Under 18","Age 18 to 35","Age 36 to 50","Age 51 to 70","70 and above"]

},
{
"button_type":"2",
"question": "How old are you",
"options": ["Under 18","Age 18 to 35","Age 36 to 50","Age 51 to 70","70 and above"]

}
]
}
]
}


This is my json data.I need to display the data in the tableview as below screen shot.



This is the screen shot.I need to display in this way.



Currently my code is below.


func numberOfSections(in tableView: UITableView) -> Int {

return dummyDataViewModel.numberOfSections()
print(dummyDataViewModel.numberOfSections())

}


func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

let identifier = "DummyDataHeaderCell"


var headercell: EQ_DummyDataHeader! = tableView.dequeueReusableCell(withIdentifier: identifier) as? EQ_DummyDataHeader

if headercell == nil {

tableView.register(UINib(nibName: "EQ_DummyDataHeader", bundle: nil), forCellReuseIdentifier: identifier)

headercell = tableView.dequeueReusableCell(withIdentifier: identifier) as? EQ_DummyDataHeader

}

headercell.setReviewData(reviews:self.dummyDataViewModel.titleForHeaderInSection(atsection:section))

return headercell

}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

if tableView == tableview{

return 50
}
return 20
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return dummyDataViewModel.numberOfRowsIn(section: section)

}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let model = dummyDataViewModel.titleForHeaderInSection(atsection: indexPath.section)
print(model.answerType1)

print(model.answerType1?.rawValue)

let c = model.answerType1
let cellClass = c?.cellType().getClass()
print(cellClass)
let cell = tableView.dequeueReusableCell(withIdentifier: (cellClass?.cellReuseIdentifier())!, for: indexPath) as! BaseCell
print(cell)


cell.selectionStyle = .none

let optionModel = dummyDataViewModel.datafordisplay(atindex: indexPath)
cell.setOptions(Options1: optionModel)

cell.type = c?.cellType()

cell.delegate = self
cell.textdelegate = self
cell.textdelegate1 = self

cell.datedelegateDatepicker1 = self

cell.selectDateDelegate = self
if optionModel.isSelected! {
cell.setOptions1(OptionsSelected:optionModel)
cell.TextDisplay(options:optionModel)


} else {
cell.setOptions1(OptionsisSelected:optionModel)
cell.setOptions(OptionsisSelected:optionModel)
cell.setOptions1(OptionsisSelected:optionModel)
cell.TextNotDisplay(options:optionModel)

}
print(cell.type)

if cell.type == .starratingtype{

cell.selectionStyle = .none
}
else if cell.type == .smileytype{

cell.selectionStyle = .none
}
else if cell.type == .checkboxtype{
cell.selectionStyle = .none
}
else if cell.type == .radiotype{
cell.selectionStyle = .none
}
else if cell.type == .textfieldtype{
cell.selectionStyle = .none
}

else if cell.type == .smileytype{
cell.selectionStyle = .none
}


return cell


}


In the view model:-



 func numberOfSections() -> Int{
print((datasourceModel.dataListArray?.count)!)
return (datasourceModel.dataListArray?.count)!
}

func titleForHeaderInSection(atsection section: Int) -> EQ_DummyDataModel {
print(datasourceModel.dataListArray![section])
return datasourceModel.dataListArray![section]
}

func numberOfRowsIn(section:Int) -> Int {

print( datasourceModel.dataListArray?[section].optionsModelArray.count ?? 0)
return datasourceModel.dataListArray?[section].optionsModelArray.count ?? 0

}

func datafordisplay(atindex indexPath: IndexPath) -> EQ_OptionModel{

print(datasourceModel.dataListArray![indexPath.section].optionsModelArray[indexPath.row])

return datasourceModel.dataListArray![indexPath.section].optionsModelArray[indexPath.row]
}


I am doing in mvvm .So how to implement the nested section in this code?
What are the changes needed to do in the number of sections and datasource.










share|improve this question















{
"data":[
{

"mainquestion":"was the staff",
"items":[

{
"button_type":"2",
"question": "Gender",
"options": ["Male","Female"]

},
{
"button_type":"2",
"question": "How old are you",
"options": ["Under 18","Age 18 to 35","Age 36 to 50","Age 51 to 70","70 and above"]

}
]
}, {

"mainquestion":"Please tell the feedback",
"items":[

{
"button_type":"2",
"question": "Gender",
"options": ["Male","Female"]

},
{
"button_type":"2",
"question": "How old are you",
"options": ["Under 18","Age 18 to 35","Age 36 to 50","Age 51 to 70","70 and above"]

},
{
"button_type":"2",
"question": "How old are you",
"options": ["Under 18","Age 18 to 35","Age 36 to 50","Age 51 to 70","70 and above"]

}
]
}
]
}


This is my json data.I need to display the data in the tableview as below screen shot.



This is the screen shot.I need to display in this way.



Currently my code is below.


func numberOfSections(in tableView: UITableView) -> Int {

return dummyDataViewModel.numberOfSections()
print(dummyDataViewModel.numberOfSections())

}


func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

let identifier = "DummyDataHeaderCell"


var headercell: EQ_DummyDataHeader! = tableView.dequeueReusableCell(withIdentifier: identifier) as? EQ_DummyDataHeader

if headercell == nil {

tableView.register(UINib(nibName: "EQ_DummyDataHeader", bundle: nil), forCellReuseIdentifier: identifier)

headercell = tableView.dequeueReusableCell(withIdentifier: identifier) as? EQ_DummyDataHeader

}

headercell.setReviewData(reviews:self.dummyDataViewModel.titleForHeaderInSection(atsection:section))

return headercell

}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

if tableView == tableview{

return 50
}
return 20
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return dummyDataViewModel.numberOfRowsIn(section: section)

}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let model = dummyDataViewModel.titleForHeaderInSection(atsection: indexPath.section)
print(model.answerType1)

print(model.answerType1?.rawValue)

let c = model.answerType1
let cellClass = c?.cellType().getClass()
print(cellClass)
let cell = tableView.dequeueReusableCell(withIdentifier: (cellClass?.cellReuseIdentifier())!, for: indexPath) as! BaseCell
print(cell)


cell.selectionStyle = .none

let optionModel = dummyDataViewModel.datafordisplay(atindex: indexPath)
cell.setOptions(Options1: optionModel)

cell.type = c?.cellType()

cell.delegate = self
cell.textdelegate = self
cell.textdelegate1 = self

cell.datedelegateDatepicker1 = self

cell.selectDateDelegate = self
if optionModel.isSelected! {
cell.setOptions1(OptionsSelected:optionModel)
cell.TextDisplay(options:optionModel)


} else {
cell.setOptions1(OptionsisSelected:optionModel)
cell.setOptions(OptionsisSelected:optionModel)
cell.setOptions1(OptionsisSelected:optionModel)
cell.TextNotDisplay(options:optionModel)

}
print(cell.type)

if cell.type == .starratingtype{

cell.selectionStyle = .none
}
else if cell.type == .smileytype{

cell.selectionStyle = .none
}
else if cell.type == .checkboxtype{
cell.selectionStyle = .none
}
else if cell.type == .radiotype{
cell.selectionStyle = .none
}
else if cell.type == .textfieldtype{
cell.selectionStyle = .none
}

else if cell.type == .smileytype{
cell.selectionStyle = .none
}


return cell


}


In the view model:-



 func numberOfSections() -> Int{
print((datasourceModel.dataListArray?.count)!)
return (datasourceModel.dataListArray?.count)!
}

func titleForHeaderInSection(atsection section: Int) -> EQ_DummyDataModel {
print(datasourceModel.dataListArray![section])
return datasourceModel.dataListArray![section]
}

func numberOfRowsIn(section:Int) -> Int {

print( datasourceModel.dataListArray?[section].optionsModelArray.count ?? 0)
return datasourceModel.dataListArray?[section].optionsModelArray.count ?? 0

}

func datafordisplay(atindex indexPath: IndexPath) -> EQ_OptionModel{

print(datasourceModel.dataListArray![indexPath.section].optionsModelArray[indexPath.row])

return datasourceModel.dataListArray![indexPath.section].optionsModelArray[indexPath.row]
}


I am doing in mvvm .So how to implement the nested section in this code?
What are the changes needed to do in the number of sections and datasource.







ios json swift mvvm uitableviewsectionheader






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 9:27









Pankaj

312513




312513










asked Nov 22 at 8:34









clydececiljohn

55




55












  • how to implement it?
    – clydececiljohn
    Nov 22 at 10:02










  • How to implement the nested sections in the tableview?
    – clydececiljohn
    Nov 22 at 10:45


















  • how to implement it?
    – clydececiljohn
    Nov 22 at 10:02










  • How to implement the nested sections in the tableview?
    – clydececiljohn
    Nov 22 at 10:45
















how to implement it?
– clydececiljohn
Nov 22 at 10:02




how to implement it?
– clydececiljohn
Nov 22 at 10:02












How to implement the nested sections in the tableview?
– clydececiljohn
Nov 22 at 10:45




How to implement the nested sections in the tableview?
– clydececiljohn
Nov 22 at 10:45

















active

oldest

votes











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',
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426776%2fnested-sections-in-the-tableview-in-swift%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426776%2fnested-sections-in-the-tableview-in-swift%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)