This struct in HomeVC:
struct JsonData{
var name: String = ""
var categoryname: String = ""
var customerdetails: [cDetails] = [cDetails]()
init(name: String, categoryname: String, customerdetails: [cDetails]){
self.name = name
self.categoryname = categoryname
self.customerdetails = customerdetails
}
}
struct cDetails{
var dValue: String = ""
init(dValue: String) {
self.dValue = dValue
}
}
In numberOfRowsInSection
if categoryname is Mobile i need extra row in tableview and in cellForRowAtindexPath i need its text cell?.searchTextfield.text = "Amount" like below how to do that?
if section == 0 {
if categoryname == "Mobile"{
//here i need extra row with cell?.searchTextfield.text = "Amount"
}
else
{
return selectedDetail?.customerdetails.count ?? 0
}
}
below is code for present view controller, please help me in the below code.
class NewSearchViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {
@IBOutlet weak var tableView: UITableView!
var cell : DetailTableViewCell?
var selectedDetail: JsonData?
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return selectedDetail?.customerdetails.count ?? 0
}
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
cell = tableView.dequeueReusableCell(withIdentifier: "textfieldCell", for: indexPath) as! DetailTableViewCell
cell?.searchTextfield.delegate = self
if let value = selectedDetail?.customerdetails[indexPath.row] {
cell?.searchTextfield.text = value.dValue
} else {
cell?.searchTextfield.text = "missing data"
}
} else if indexPath.section == 1 {
cell = tableView.dequeueReusableCell(withIdentifier: "buttonCell", for: indexPath) as! DetailTableViewCell
}
return cell!
}
}
Please help me in the code.
try this
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
if categoryname == "Mobile"{
return selectedDetail?.customerdetails.count + 1 ?? 0
}
else
{
return selectedDetail?.customerdetails.count ?? 0
}
}
}
then
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
cell = tableView.dequeueReusableCell(withIdentifier: "textfieldCell", for: indexPath) as! DetailTableViewCell
cell?.searchTextfield.delegate = self
//play with this validation
if selectedDetail?.customerdetails.count - 2 >= indexPath.row
{
if let value = selectedDetail?.customerdetails[indexPath.row] {
cell?.searchTextfield.text = value.dValue
} else {
cell?.searchTextfield.text = "missing data"
}
}
else
{
cell?.searchTextfield.text = "Amount"
}
} else if indexPath.section == 1 {
cell = tableView.dequeueReusableCell(withIdentifier: "buttonCell", for: indexPath) as! DetailTableViewCell
}
return cell!
}
Somehow i can't add a comment so i will respond with this answer.
For what i can see, you're returning 2 in func numberOfSections(in tableView: UITableView)
.
I think you need to find a way to program this dynamically. For example, if you store all the data you need in an array. you can simply return arrayName.count
So i think you need to write a function to store all the data you need in an array. and return this in your TableView.
I hope i understood your problem well, and this answer made sense.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.