我已经以编程方式创建了一个表视图,下面是代码,当我尝试使用完成处理程序更新标签时,该值不会显示在表视图单元格中。
有人可以建议我如何在tableview中的 languagesSpokenCell 中更新textLabel吗?我花了几个小时试图解决这个问题,但我仍然无法解决。
这是在用户选择他所说的语言后调用的完成处理程序。
func showLanguagesSpoken(_ languagesSpoken: [String]?){
languagesSpokenString = (languagesSpoken?.joined(separator: ", "))!
languagesSpokenCell.textLabel?.text = languagesSpokenString
//*** In the below print statement the value is printed correctly but the text label is not updated in the cell.
print("languagesSpokenCell.textLabel?.text: \(languagesSpokenCell.textLabel?.text)")
self.tableView.reloadData()
}
这里我以编程方式创建表格视图单元格
// FOR TABLE VIEW - Tableview cells
var tableView: UITableView = UITableView()
var firstNameCell: UITableViewCell = UITableViewCell()
var lastNameCell: UITableViewCell = UITableViewCell()
var languagesSpokenCell: UITableViewCell = UITableViewCell()
// FOR TABLE VIEW - Textfields
var firstName: UITextField = UITextField()
var lastName: UITextField = UITextField()
override func loadView() {
super.loadView()
// construct first name cell, section 0, row 0
self.firstNameCell.backgroundColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.5)
self.firstName = UITextField(frame: self.firstNameCell.contentView.bounds.insetBy(dx: 15, dy: 0))
self.firstName.placeholder = "First Name"
self.firstNameCell.addSubview(self.firstName)
// construct last name cell, section 0, row 1
self.lastNameCell.backgroundColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.5)
self.lastName = UITextField(frame: self.lastNameCell.contentView.bounds.insetBy(dx: 15, dy: 0))
self.lastName.placeholder = "Last Name"
self.lastNameCell.addSubview(self.lastName)
self.languagesSpokenCell.textLabel?.text = "Languages Spoken"
self.languagesSpokenCell.backgroundColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 0.5)
self.languagesSpokenCell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
}
以下是表格视图方法
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
// Return the number of rows for each section in your static table
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch(section) {
case 0: return 2 // section 0 has 2 rows
case 1: return 1 // section 1 has 1 row
default: fatalError("Unknown number of sections")
}
}
// Return the row for the corresponding section and row
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch(indexPath.section) {
case 0:
switch(indexPath.row) {
case 0: return self.firstNameCell // section 0, row 0 is the first name
case 1: return self.lastNameCell // section 0, row 1 is the last name
default: fatalError("Unknown row in section 0")
}
case 1:
switch(indexPath.row) {
case 0: return self.languagesSpokenCell
}
default: fatalError("Unknown section")
}
}
// Customize the section headings for each section
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
switch(section) {
case 0: return "Profile"
case 1: return "Languages Spoken"
default: fatalError("Unknown section")
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let row = indexPath.row
let section = indexPath.section
let currentCell = tableView.cellForRow(at: indexPath) as! UITableViewCell
if section == 4 && row == 0 {
// The user has clicked on languages spoken cell
self.performSegue(withIdentifier: "showLanguageSelectionTVC", sender: self)
}
print("Printing celll text label: \(currentCell.textLabel!.text)")
}
以下是我在ViewDidLoad()
方法中设置的约束。
tableView = UITableView(frame: CGRect.zero, style: UITableViewStyle.grouped)
tableView.delegate = self
tableView.dataSource = self
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.view.addSubview(self.tableView)
// Disabling automatic constraints
tableView.translatesAutoresizingMaskIntoConstraints = false
// Do any additional setup after loading the view.
let viewDict = [
"tableView" : tableView
]
// Setting Constraints for the table view
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[tableView]|", options: [], metrics: nil, views: viewDict))
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[tableView]|", options: [], metrics: nil, views: viewDict))
答案 0 :(得分:0)
要更新tableview中的任何单元格,您需要重新加载它。您可以为每个单元格或特定单元格或整个tableview重新加载它。在重新加载tableview之前,请确保更新数据源。
在这个showLanguagesSpoken
方法中,您尝试更改单元格的文本而不更新tableView。
另请确保始终更改cellForRowAtIndexpath
答案 1 :(得分:0)
从以下方法中删除reloadData。
func showLanguagesSpoken(_ languagesSpoken: [String]?){
languagesSpokenString = (languagesSpoken?.joined(separator: ", "))!
languagesSpokenCell.textLabel?.text = languagesSpokenString
//*** In the below print statement the value is printed correctly but the text label is not updated in the cell.
print("languagesSpokenCell.textLabel?.text: \(languagesSpokenCell.textLabel?.text)")
}
答案 2 :(得分:0)
标签languagesSpokenCell.textLabel
文字更新流程应位于主线程。
func showLanguagesSpoken(_ languagesSpoken: [String]?) {
DispatchQueue.main.async {
languagesSpokenString = (languagesSpoken?.joined(separator: ", "))!
languagesSpokenCell.textLabel?.text = languagesSpokenString
print("languagesSpokenCell.textLabel?.text: \(languagesSpokenCell.textLabel?.text)")
}
}