Ios 如何使用textfield text“向tableview添加额外的行;“金额”;迅速地
HomeVC中的此结构:Ios 如何使用textfield text“向tableview添加额外的行;“金额”;迅速地,ios,swift,count,uitextfield,tableview,Ios,Swift,Count,Uitextfield,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 = cat
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
中,如果categoryname是移动的,我需要在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 numberOfSections(在tableView:UITableView)
中返回了2
我认为您需要找到一种动态编程的方法。例如,如果将所需的所有数据存储在一个数组中。您只需返回arrayName.count
因此,我认为您需要编写一个函数来存储数组中所需的所有数据。并将其返回到TableView中
我希望我能很好地理解你的问题,这个答案是有道理的 请帮忙输入密码
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!
}