如何在 swift 中使用文本字段文本 "Amount" 添加额外的行到表视图
How to add extra row to tableview with textfield text "Amount" in swift
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
}
}
在 numberOfRowsInSection
中,如果类别名称是 Mobile,我需要在 tableview 和 cellForRowAtindexPath 中添加额外的行,我需要它的文本单元格吗?searchTextfield.text = "Amount"
像下面这样怎么做?
if section == 0 {
if categoryname == "Mobile"{
//here i need extra row with cell?.searchTextfield.text = "Amount"
}
else
{
return selectedDetail?.customerdetails.count ?? 0
}
}
下面是当前视图控制器的代码,请在下面的代码中帮助我。
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!
}
}
请帮我写代码。
试试这个
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
}
}
}
然后
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!
}
不知怎么的,我无法添加评论,所以我会回复这个答案。
据我所知,您 returning 2 in func numberOfSections(in tableView: UITableView)
。
我认为您需要找到一种动态编程的方法。例如,如果您将需要的所有数据存储在一个数组中。你可以简单地 return arrayName.count
所以我认为您需要编写一个函数来将您需要的所有数据存储在一个数组中。 return 在您的 TableView 中。
希望我能很好地理解您的问题,并且这个答案是有道理的。
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
}
}
在 numberOfRowsInSection
中,如果类别名称是 Mobile,我需要在 tableview 和 cellForRowAtindexPath 中添加额外的行,我需要它的文本单元格吗?searchTextfield.text = "Amount"
像下面这样怎么做?
if section == 0 {
if categoryname == "Mobile"{
//here i need extra row with cell?.searchTextfield.text = "Amount"
}
else
{
return selectedDetail?.customerdetails.count ?? 0
}
}
下面是当前视图控制器的代码,请在下面的代码中帮助我。
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!
}
}
请帮我写代码。
试试这个
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
}
}
}
然后
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!
}
不知怎么的,我无法添加评论,所以我会回复这个答案。
据我所知,您 returning 2 in func numberOfSections(in tableView: UITableView)
。
我认为您需要找到一种动态编程的方法。例如,如果您将需要的所有数据存储在一个数组中。你可以简单地 return arrayName.count
所以我认为您需要编写一个函数来将您需要的所有数据存储在一个数组中。 return 在您的 TableView 中。
希望我能很好地理解您的问题,并且这个答案是有道理的。