首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从VC到UILabel的插座无效。Outlet不能连接到重复内容

从VC到UILabel的插座无效。Outlet不能连接到重复内容
EN

Stack Overflow用户
提问于 2017-08-03 03:38:40
回答 2查看 458关注 0票数 0

我正在开发一个待办事项列表应用程序,可以在Swift中使用表格视图。表视图有一个名为titleLabelOutlet的UILabel。当我构建应用程序时,我收到一个错误,说The titleLabelOutlet outlet from the FirstViewController to the UILabel is invalid. Outlets cannot be connected to repeating content.,我以前从未收到过这个错误。我该如何修复它?这是我的代码:

代码语言:javascript
复制
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.
}


}

EN

回答 2

Stack Overflow用户

发布于 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 =“任意文本”返回单元格

`

票数 0
EN

Stack Overflow用户

发布于 2017-08-03 04:24:06

创建CustomTableCell子类化UITableViewCell.

在单元格中有标签,并连接到CustomTableCell类中的phoneNumberLabelOutlet & titleLabelOutlet。

在cellForRowAtIndex函数的UITableViewCell中使用CustomTableCell。现在,您可以将标注的值设置为

cell.phoneNumberLabelOutlet.text = phoneNumberListindexPath.row cell.titleLabelOutlet.text = titleFieldListindexPath.row

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45469581

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档