【问题标题】:How to create controls dynamically and aligned dynamically in swift 4?如何在swift 4中动态创建控件并动态对齐?
【发布时间】:2019-02-11 15:19:47
【问题描述】:

我正在使用 Swift 4 开发 iOS 应用程序。在那个项目中,我有这样的要求,我必须动态创建控件以及正确的对齐方式。

例如,我有一个按钮,当我单击该按钮时,我正在点击服务,我正在获取包含 4 个对象的 json 数据。基于此,我必须动态创建控件并且动态对齐也应该这样做。我尝试了很多示例和教程。我没有找到任何解决方案。

【问题讨论】:

  • 你能详细说明吗dynamic alignment also should do
  • 就像,在一个用户中,我将获得 4 个 json 对象。基于此,我必须创建 4 个标签和 Textfield 以及一个提交按钮。对于第二个客户 8 数据,我将根据该数组的计数来动态创建控件。我现在清楚了吗?
  • 到目前为止你尝试过什么?你能不能给我们看一些代码,让我们看看实际的问题是什么?
  • 你看过UITableView吗?
  • 尝试@Losiowaty 回答

标签: ios swift3 uitextfield


【解决方案1】:

您可以为此使用 UITableView,这里是示例:

import UIKit

class ViewController: UIViewController {
@IBOutlet weak var tableview: UITableView!
var nameArr :[String] = []
override func viewDidLoad() {
    super.viewDidLoad()
    tableview.delegate  =  self
    tableview.dataSource = self
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
@IBAction func four_btn(_ sender: Any) {
    nameArr.removeAll()
    let nameData = ["First Name","Middle Name","Last Name","DOB"]
    nameArr += nameData
    tableview.reloadData()
}

@IBAction func eight_btn(_ sender: Any) {
    nameArr.removeAll()
    let nameData = ["Salutation","First Name","Middle Name","Last Name","DOB","Gender","Mobile","Email"]
    nameArr += nameData
    tableview.reloadData()
}

}
extension ViewController: UITableViewDataSource, UITableViewDelegate {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return nameArr.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! tableviewCells
    cell.nameLabel.text = nameArr[indexPath.row]
    return cell
}


}
class tableviewCells : UITableViewCell{
@IBOutlet weak var nameLabel: UILabel!

}

【讨论】:

  • 这很好!! !
【解决方案2】:

你也可以使用 UITableView 您的情况就像,一个用户可能有 5 条记录,而另一个用户可能有 10 或 12 条记录,这意味着您必须动态工作

如果有 2 个按钮调用 2 个不同的 API,那么只需像这样管理 2 个不同的数组

var arr1 = NSArray()
var arr2 = NSArray()
var isAPI1Called = Bool()

将两个api的响应保存在不同的数组中

然后只需在按钮点击和像这样的合适视图中管理标志

@IBAction func btn1(_ sender: Any) {
 isAPI1Called = true
self.API1Called()
}

@IBAction func btn2(_ sender: Any) {
isAPI1Called = false
self.API1Called()
}

现在像这样在 UITableview Delegate 中使用标志

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if isAPI1Called
    {
    return arr1.count
    }
    else
    { 
     return arr2.count
    }

}

如果 UI 发生变化,请根据您的要求加载 UITableviewCell

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
         if isAPI1Called
         {
          let cell = tableView.dequeueReusableCell(withIdentifier: "Cell1", for: indexPath) as! UITableviewCell
         //Do your required stuff here
         return cell
         }
         else
         {
          let cell = tableView.dequeueReusableCell(withIdentifier: "Cell1", for: indexPath) as! UITableviewCell
         //Do your required stuff here
         return cell
         }

}

希望对你有帮助 如果没有得到任何点,请评论

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-22
    • 2013-03-15
    • 2015-12-17
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多