-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{
}
}
}
제품의 데이터를 두 번 업데이트하고 싶습니다. 그러나 내 데이터를 두 번 업데이트하고 tableView.reloadData()라고하는 경우 마지막 데이터 만 표시됩니다. 첫 번째 데이터가 테이블에 나타나지 않습니다. – Abhishek
'reloadData()'를 호출 한 후'products'의 업데이트 데이터가 tableView에 나타나지 않는다고 말하고 있습니까? – LFHS
self.products = [ "d1", "d2", "d3", "d4"]이 (가) tableview에 나타납니다. 그러나 self.products = [ "r1", "r2", "r3", "r4"]는 tableview에 나타나지 않습니다. – Abhishek