我正在开发一个待办事项列表应用程序,可以在Swift中使用表格视图。表视图有一个名为titleLabelOutlet的UILabel。当我构建应用程序时,我收到一个错误,说The titleLabelOutlet outlet from the FirstViewController to the UILabel is invalid. Outlets cannot be connected to repeating content.,我以前从未收到过这个错误。我该如何修复它?这是我的代码:
import UIKit
var phoneNumberList = [String]()
var titleFieldList = [String]()
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var titleLabelOutlet: UILabel!
@IBOutlet weak var phoneNumberLabelOutlet: UILabel!
@IBOutlet weak var myTableView: UITableView!
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return (phoneNumberList.count)
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
phoneNumberLabelOutlet.text = phoneNumberList[indexPath.row]
titleLabelOutlet.text = titleFieldList[indexPath.row]
return(cell)
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
{
if editingStyle == UITableViewCellEditingStyle.delete
{
phoneNumberList.remove(at: indexPath.row)
titleFieldList.remove(at: indexPath.row)
myTableView.reloadData()
}
}
override func viewDidAppear(_ animated: Bool) {
myTableView.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

发布于 2017-08-03 03:59:37
您的outlets应该在cell类中,而不是viewcontroller中,因为标签在cell中,而在委托的crllfor行中,您必须像‘get cell = tableView.dequeueReusableCell(withReuseIdentifier: cellIdentifier“那样获取outlets,for: indexPath) as!TableViewCellClass
cell.lbloutlet.text =“任意文本”返回单元格
`
发布于 2017-08-03 04:24:06
创建CustomTableCell子类化UITableViewCell.
在单元格中有标签,并连接到CustomTableCell类中的phoneNumberLabelOutlet & titleLabelOutlet。
在cellForRowAtIndex函数的UITableViewCell中使用CustomTableCell。现在,您可以将标注的值设置为
cell.phoneNumberLabelOutlet.text = phoneNumberListindexPath.row cell.titleLabelOutlet.text = titleFieldListindexPath.row
https://stackoverflow.com/questions/45469581
复制相似问题