我正在try 从nib创建一个自定义表视图单元格.我指的是第here条.我面临两个问题.

我创建了一个.xib文件,其中拖放了一个UITableViewCell对象.我创建了一个UITableViewCell的子类,并将其设置为单元的类,将Cell设置为可重用标识符.

import UIKit

class CustomOneCell: UITableViewCell {

    @IBOutlet weak var middleLabel: UILabel!
    @IBOutlet weak var leftLabel: UILabel!
    @IBOutlet weak var rightLabel: UILabel!

    required init(coder aDecoder: NSCoder!) {
        super.init(coder: aDecoder)
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

在UITableViewController中,我有以下代码,

import UIKit

class ViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate {

    var items = ["Item 1", "Item2", "Item3", "Item4"]

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    // MARK: - UITableViewDataSource
    override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }

    override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
        let identifier = "Cell"
        var cell: CustomOneCell! = tableView.dequeueReusableCellWithIdentifier(identifier) as? CustomOneCell
        if cell == nil {
            tableView.registerNib(UINib(nibName: "CustomCellOne", bundle: nil), forCellReuseIdentifier: identifier)
            cell = tableView.dequeueReusableCellWithIdentifier(identifier) as? CustomOneCell
        }

        return cell
    }
}

这段代码没有任何错误,但是当我在模拟器中运行它时,它看起来是这样的.

在此处输入图像描述

在情节提要中的UITableViewController中,我没有对单元格做任何操作.标识符为空,没有子类.我try 向原型单元添加Cell标识符,并再次运行它,但得到相同的结果.

我遇到的另一个错误是,当我试图在UITableViewController中实现以下方法时.

override func tableView(tableView: UITableView!, willDisplayCell cell: CustomOneCell!, forRowAtIndexPath indexPath: NSIndexPath!) {

    cell.middleLabel.text = items[indexPath.row]
    cell.leftLabel.text = items[indexPath.row]
    cell.rightLabel.text = items[indexPath.row]
}

如我提到的文章中所示,我将cell参数的类型从UITableViewCell更改为CustomOneCell,这是我的UITableViewCell的子类.但是我得到以下错误,

Overriding method with selector 'tableView:willDisplayCell:forRowAtIndexPath:' has incompatible type '(UITableView!, CustomOneCell!, NSIndexPath!) -> ()'

有人知道如何解决这些错误吗?这些在Objective-C中似乎很有效.

谢谢.

编辑:我刚刚注意到如果我把模拟器的方向改成横向,再把它调回纵向,单元格就会出现!我还是弄不明白发生了什么事.如果您有时间快速浏览一下,我上传了一个Xcode项目here,演示了这个问题.

推荐答案

对于SWIFT 5和IOS 12.2,您应该try 以下代码来解决您的问题:

CustomCell.swift

import UIKit

class CustomCell: UITableViewCell {

    // Link those IBOutlets with the UILabels in your .XIB file
    @IBOutlet weak var middleLabel: UILabel!
    @IBOutlet weak var leftLabel: UILabel!
    @IBOutlet weak var rightLabel: UILabel!

}

TableViewController.swift

import UIKit

class TableViewController: UITableViewController {

    let items = ["Item 1", "Item2", "Item3", "Item4"]

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "CustomCell")
    }

    // MARK: - UITableViewDataSource

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell

        cell.middleLabel.text = items[indexPath.row]
        cell.leftLabel.text = items[indexPath.row]
        cell.rightLabel.text = items[indexPath.row]

        return cell
    }

}

下图显示了一组约束,这些约束与提供的代码一起使用,没有来自Xcode的任何约束模糊性消息:

enter image description here

Ios相关问答推荐

两次调度Deliverc时未调用iOS GCD完成块

ID测试在集合中发生多次,这将给出未定义的结果

无法在fastlane和github操作中从react-native 应用程序构建ios

UIImage大小调整工作,但转换为上传数据时恢复到原始大小

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

快速相机放大手势不能正常工作

IOS-获取本地化系统映像

Flutter应用内购买无法恢复购买

.frame() 修饰符的位置如何影响视图?

UIView 倒置旋转时的zoom 问题

iOS SwiftUI - 使用 PhotosPicker 从图库中 Select 图像或视频

xcode-select:找不到clang

在 SwiftUI 中使用 .rotation3DEffect 在视图之间翻转

如何使用 AppIntents 在 SwiftUI 应用程序中打开特定视图

SwiftUI - 在ForEach的每个元素之间自动添加分隔符

如何从 scendelegate 打开我的标签栏控制器

CocoaPods 找不到 podFirebase/Core的兼容版本 | cloud_firestore, Flutter

有没有办法在 xib 文件中的视图和顶部布局指南之间添加约束?

使用 Swift 在一个 ViewController 中强制横向模式

如果我的分发证书过期会怎样?