【问题标题】:Connect UITableViewCell content to outlet in code在代码中将 UITableViewCell 内容连接到插座
【发布时间】:2018-08-14 06:48:55
【问题描述】:

我有一个 UITableView,里面有一个 UITableViewCell,里面有一个 textview 和 imageview

在我定义的 swift 文件中

class NotificationCell: UITableViewCell {
@IBOutlet weak var notificationAvatarImage: UIImageView!
@IBOutlet weak var notificationText: UITextView!


override func awakeFromNib() {
    notificationAvatarImage.clipsToBounds = true
    notificationAvatarImage.contentMode = .scaleAspectFill
}
}

我无法将情节提要中的 imageView 和 textview 连接到 NotificationCell 中的 IBOutlet notificationAvatarImage ...。我该怎么做?

我将表格加载为

guard let notificationCell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? NotificationCell else {
        fatalError("Error getting cell")
}

【问题讨论】:

标签: ios swift


【解决方案1】:

首先使用tableview的UITableViewDataSource委托来加载tableviewcell。

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! NotificationCell
    return cell
}

使用故事板

现在在情节提要中创建 UITableViewCell 并将该 UITableViewCell 的 Class 设置为 NotificationCell 然后右键单击情节提要中 tableviewcell 的 imageview 和 textview 并将其分配给 NotificationCell 类的 @IBOutlet你用 Swift 写的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-21
    • 2015-10-11
    • 2015-01-24
    • 2020-01-25
    • 2015-07-12
    相关资源
    最近更新 更多