无法将XIB加载到可重用的TableViewCell中

问题描述

我创建了一个XIB文件及其控制器来处理可重用的自定义单元格

enter image description here

并设置我的XIB的班级所有者:

enter image description here

class WallTableCell: UITableViewCell {
    //Outlets
    
    func setupCell() {
        setupLabels()
    }
    
    func setupLabels() {
        self.title.text = "Sample Title"
        self.postDescriptionLabel.text = "Sample Description"
    }
}

I,当前有2个情节提要,其中第一个使用NavigationView指向第二个情节提要,第二个包含TabBarView,其中第一个项目带有{{1} }。

在这里,我在情节提要中添加TableView

enter image description here

该单元格的类别设置为:

enter image description here

并且控制器的类设置为:

enter image description here

我在哪里注册笔尖并尝试在UITableViewCell方法上使用它:

cellForRowAt

但是,尽管如此,当我在模拟器中运行该应用程序时,仍然会出现一个空单元格。

enter image description here

我该怎么做才能在tableViewCell中显示我的自定义XIB视图?

按照@Sh_Khan的回答并添加

class WallViewController: UIViewController {
    @IBOutlet weak var messagesTable: UITableView!
    
    override func viewDidLoad() {
        loadNibForCells()
    }
    
    func loadNibForCells() {
        let nib = UINib(nibName: "WallTableCell",bundle: nil)
        messagesTable.register(nib,forCellReuseIdentifier: "wallCell")
    }
}

extension WallViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let wallCell = messagesTable.dequeueReusableCell(withIdentifier: "wallCell") as? WallTableCell else {
            return UITableViewCell()
        }
        
        wallCell.setupCell()
        
        return wallCell
    }
}

该应用程序崩溃并显示错误

messagesTable.delegate = self
messagesTable.dataSource = self

我认为这与以下事实有关:情节提要中的Thread 1: Exception: "[<NSObject 0x600001628e70> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key postDescriptionLabel" 是空的,而不是包含TableViewCell的组件

解决方法

请确保设置dataSource并在需要时进行委托

@SuppressWarnings("unhecked")
public static boolean areStructuralEqual(Map<String,?> left,Map<String,?> right) {
    Set<Entry<String,?>> leftEntriesWithNonNullValues = 
            extractEntriesWithNonNullValues(left);
    Set<Entry<String,?>> rightEntriesWithNonNullValues =
            extractEntriesWithNonNullValues(right);
    Set<String> leftKeysWithNonNullValues = leftEntriesWithNonNullValues.stream()
            .map(Entry::getKey)
            .collect(Collectors.toUnmodifiableSet());
    Set<String> rightKeysWithNonNullValues = rightEntriesWithNonNullValues.stream()
            .map(Entry::getKey)
            .collect(Collectors.toUnmodifiableSet());
    if (!Objects.equals(leftKeysWithNonNullValues,rightKeysWithNonNullValues)) {
        return false;
    }

    Set<String> leftKeysWithMapValues =
            extractAllKeysThatMapToMaps(leftEntriesWithNonNullValues);
    Set<String> rightKeysWithMapValues =
            extractAllKeysThatMapToMaps(rightEntriesWithNonNullValues);
    if (!Objects.equals(leftKeysWithMapValues,rightKeysWithMapValues)) {
        return false;
    }

    for (String key : leftKeysWithMapValues) {
        if (!areStructuralEqual(
                (Map<String,?>) left.get(key),(Map<String,?>) right.get(key))) {
            return false;
        }
    }
    return true;
}

private static Set<String> extractAllKeysThatMapToMaps(Set<Entry<String,?>> entrySet) {
    return entrySet.stream()
            .filter(e -> e.getValue() instanceof Map)
            .map(Entry::getKey)
            .collect(Collectors.toUnmodifiableSet());
}

private static Set<Entry<String,?>> extractEntriesWithNonNullValues(Map<String,?> map) {
    return map.entrySet().stream()
            .filter(e -> Objects.nonNull(e.getValue()))
            .collect(Collectors.toUnmodifiableSet());
}
,

有一个错误:Thread 1: Exception: "[<NSObject 0x600001628e70> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key postDescriptionLabel",所以也许您需要取消绑定postDescriptionLabel并将其从xib文件重新绑定到swift文件。

,

虽然我缺少添加Sh_Khan建议的messagesTable的数据源,但后来针对我的错误的解决方案是我的参考出口指向“文件的所有者”,而不是文件的出口。

因此,在选择自定义TableViewCell(而不是原型)时,应该是:

enter image description here

由于某种原因,它不是右侧的“标题标签”,而是“文件的所有者”

原型单元格中不应包含任何附加类。