2017-07-31 48 views
1

我有一個設置爲動態文本字段的視圖。從我的API JSON對象傳遞到基於JSON對象值的視圖,文本字段應該動態顯示。我使用故事板,而不是以編程方式創建視圖。如何在swift中創建動態的UItextfields?

json = [{"sydneyTomelbourn":500$}, 
     {"sydneyToHampshre":100$}, 
     {"sydneyToUSA":200$} 
     ] 

這些價格是可編輯的。並且應該顯示在文本字段上。 有什麼建議嗎?

+0

可以使用的tableView – Jaydeep

+0

在創建自定義單元格的UITableView –

+1

for循環,不斷添加文本框,該文本框頂部將永遠被計算爲我*(一些常數),並保持該標籤爲我 – YaBoiSandeep

回答

-1

我認爲這是很容易只是創建自定義單元格類來實現這一目標

第1步:創建JSON響應的陣列

說arrayForPrice它包含所有的價格在給定的JSON

第2步:創建自定義類tableViewCell

class InputDataCell: UITableViewCell 
{ 
    @IBOutlet weak var txtItemPrice: UITextField! 
    override func awakeFromNib() { 
     super.awakeFromNib() 

    } 

    override func setSelected(_ selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 

     // Configure the view for the selected state 
    } 
} 

STEP:3分配價格從陣列uitextFieald在cellForRowMethod 像

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
let inputDataCell:InputDataCell = (tableView.dequeueReusableCell(withIdentifier: "inputItemCell", for: indexPath) as? InputDataCell)! //InputDataCell is the Custom class For uitableViewCell 

       inputDataCell.txtItemPrice.text = arrayForPrice[indexPath.row] 

     return inputDataCell 
}