2017-11-05 12 views
-3

다른 데이터가 포함 된 여러 행이있는 동적으로 테이블보기를 만들고 싶습니다. 나는 신속하게 새로운 행을 만드는 법을 모른다. 3 행을 만들고 각 행에 다른 데이터를 추가하려고합니다. 배열 데이터를 업데이트하고 테이블 뷰를 다시로드 할 수 있다는 것을 알고 있습니다. 그러나 루프에서 데이터를 업데이트하고 테이블 뷰를 다시로드하면 마지막 데이터 만 표시됩니다. 당신이 products에서 데이터를 업데이트 할 경우 전 = 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

답변

0

, 당신이 호출 할 수 있습니다

VAR : 인덱스 2. 에서 루프에서 추가의 tableview 누락 된 데이터이 내 코드입니다 tableView.reloadData() 모든 tableView 함수를 다시 호출합니다. 그것들을 당신이 원하는 것을 반영하도록 설정했다면, 이것은 새로운 행을 만들어야합니다.

+0

제품의 데이터를 두 번 업데이트하고 싶습니다. 그러나 내 데이터를 두 번 업데이트하고 tableView.reloadData()라고하는 경우 마지막 데이터 만 표시됩니다. 첫 번째 데이터가 테이블에 나타나지 않습니다. – Abhishek

+0

'reloadData()'를 호출 한 후'products'의 업데이트 데이터가 tableView에 나타나지 않는다고 말하고 있습니까? – LFHS

+0

self.products = [ "d1", "d2", "d3", "d4"]이 (가) tableview에 나타납니다. 그러나 self.products = [ "r1", "r2", "r3", "r4"]는 tableview에 나타나지 않습니다. – Abhishek