簡體   English   中英

如何在 swift 中使用文本字段文本“金額”向 tableview 添加額外的行

[英]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中,如果 categoryname 是 Mobile,我需要在 tableview 和 cellForRowAtindexPath 中添加額外的行,我需要它的文本 cell?.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
      }
    }

下面是當前視圖 controller 的代碼,請在下面的代碼中幫助我。

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!
}

不知何故,我無法添加評論,所以我會回復這個答案。

對於我所看到的,您在func numberOfSections(in tableView: UITableView)返回 2 。

我認為您需要找到一種動態編程的方法。 例如,如果您將所需的所有數據存儲在一個數組中。 您可以簡單地返回arrayName.count

所以我認為你需要編寫一個 function 來將你需要的所有數據存儲在一個數組中。 並在您的 TableView 中返回它。

我希望我能很好地理解你的問題,這個答案是有道理的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM