如何在swift中创建动态的UItextfields?

问题描述:

我有一个设置为动态文本字段的视图。从我的API JSON对象传递到基于JSON对象值的视图,文本字段应该动态显示。我使用故事板,而不是以编程方式创建视图。如何在swift中创建动态的UItextfields?

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

这些价格是可编辑的。并且应该显示在文本字段上。 有什么建议吗?

+0

可以使用的tableView – Jaydeep

+0

在创建自定义单元格的UITableView –

+1

for循环,不断添加文本框,该文本框顶部将永远被计算为我*(一些常数),并保持该标签为我 – YaBoiSandeep

我认为这是很容易只是创建自定义单元格类来实现这一目标

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