如何在swift中创建多行动态表视图?

时间:2017-11-05 19:10:04

标签: ios swift dynamic tableview

我想创建包含多行的动态表视图,其中包含不同的数据。我不知道如何在swift中创建新行。我想创建3行并向每行添加不同的数据。我知道我可以更新数组数据并重新加载tableview。但是当我在循环中更新数据并重新加载tableview时,只显示最后的数据。 Tableview在索引2处的for循环中添加的缺失数据。 这是我的代码:

var i = 0

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    tableView.frame = CGRect(x: 5, y:200 ,width: self.view.frame.width, height: 400)
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
    tableView.delegate = self
    tableView.dataSource = self
    tableView.layer.borderWidth = 2

    self.view.addSubview(tableView)
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return products.count
}
func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)


    let tf = UITextField(frame: CGRect(x: 320, y: 10*i , width: 300, height:20))
    tf.placeholder = "_"
    tf.tag = indexPath.row
    tf.font = UIFont.systemFont(ofSize: 15)

    let pest = UILabel(frame: CGRect(x: 10, y: 10*i, width: 300, height: 60))

     pest.font = UIFont.systemFont(ofSize: 15)
     pest.numberOfLines = 0
     pest.text = self.products[indexPath.row]

    let pp = UILabel()
    pp.text = "PP"


    cell.contentView.addSubview(pest)
    cell.contentView.addSubview(tf)



   return cell

}
override func viewDidAppear(_ animated: Bool) {


    createTable()
}

  func createTable(){
    for index in 1...3{
        if(index == 2){
             i = index
             self.products = ["r1","r2","r3","r4"]
             tableView.reloadData()


        }
      else  if(index == 3){

            i = index
            self.products = ["d1","d2","d3","d4"]
            tableView.reloadData()

        }
        else{

        }

    }
}

enter image description here

1 个答案:

答案 0 :(得分:0)

如果您在{ "dev": { "app_function": "hello.app", "aws_region": "us-east-1", "profile_name": null, "project_name": "flask", "runtime": "python3.6", "s3_bucket": "zappa-xxxxxxxxx", "cors": true }, "authorizer": { "arn": "arn:aws:lambda:us-east-1:xxxxxxxxxxx:function:flask-authorizer", "result_ttl": 0 } } 更新数据,则可以调用products,这将再次调用所有tableView函数。假设你已将它们设置为反映你想要的东西,那么这应该会创建一个新行。

相关问题