我对Swift 的课有意见.我有一个用于UITableViewController类和UITableViewCell类的快速文件.我的问题是UITableViewCell类和插座.这个班级有一个错误Class "HomeCell" has no initializers,我不理解这个问题.

谢谢你的回复.

import Foundation
import UIKit

class HomeTable: UITableViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet var tableViex: UITableView!

    var items: [(String, String, String)] = [
        ("Test", "123", "1.jpeg"),
        ("Test2", "236", "2.jpeg"),
        ("Test3", "678", "3.jpeg")
    ]

    override func viewDidLoad() {
        super.viewDidLoad()

        var nib = UINib(nibName: "HomeCell", bundle: nil)
        tableView.registerNib(nib, forCellReuseIdentifier: "bookCell")
    }

    // Number row
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.items.count
    }

    // Style Cell
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("bookCell") as UITableViewCell

        // Style here

        return cell

    }

    // Select row
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        // Select
    }

}

// PROBLEM HERE
class HomeCell : UITableViewCell {

    @IBOutlet var imgBook: UIImageView
    @IBOutlet var titleBook: UILabel
    @IBOutlet var pageBook: UILabel

    func loadItem(#title: String, page: String, image:String) {
        titleBook.text = title
        pageBook.text = page
        imgBook.image = UIImage(named: image)
    }

}

推荐答案

You have to use implicitly unwrapped optionals so that Swift can cope with circular dependencies (parent <-> child of the UI components in this case) during the initialization phase.

@IBOutlet var imgBook: UIImageView!
@IBOutlet var titleBook: UILabel!
@IBOutlet var pageBook: UILabel!

读了这doc条,他们都解释得很好.

Ios相关问答推荐

自定义alert 未执行任何操作

滚动时突出显示工具栏项目

由于代码签名时出错,无法在iOS设备上发布iOS Maui Build

如何在 Android 上的 Flutter 应用中录制内部音频?

在 Swift 项目中使用情节提要加载 obj-c 文件

多协议和类继承混乱,Swift

表视图(SwiftUI)中的多列?

ITMS-90432:将应用上传到应用store 时出现问题

为什么这个 SwiftUI 状态在作为非绑定参数传递时没有更新?

如何将 -fobjc-arc-exceptions 标志正确添加到 XCode 中?

Flutter 构建 ios 失败:运行 pod 安装时出错

发生异常:需要 issuerId

expo EAS build (iOS) 在 Pod 安装步骤中失败 (SDK45 & 46)

UIButton在iOS7中没有显示突出显示

通过 segue 传递数据

在 iPhone 应用程序中使用 SSL - 出口合规性

应用程序不包含正确的测试版权利

iOS7 UITextView contentsize.height 替代

有什么方法可以加粗 NSString 的一部分?

Swift - 获取设备的 WIFI IP 地址