{
"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.