Ios UITableViewCell加载了错误的特征变量

Ios UITableViewCell加载了错误的特征变量,ios,swift,uitableview,constraints,interface-builder,Ios,Swift,Uitableview,Constraints,Interface Builder,我有一个UITableViewCell,其中包含一个UICollectionView,iPhone和iPad的显示略有不同 因此,iPhone(CR)安装了一些限制,但iPad(RR)没有。 当tableViewCell显示时,它似乎与上次保存的xib状态一致,我的意思是: 如果在将xib视为IPad时将其保存,我的tableview将在IPad上正确显示,但在iPhone上不会显示 如果在将xib作为iPhone查看时将其保存,我的tableView将在iPhone上正确显示,但不会在iPa

我有一个UITableViewCell,其中包含一个UICollectionView,iPhone和iPad的显示略有不同

因此,iPhone(CR)安装了一些限制,但iPad(RR)没有。 当tableViewCell显示时,它似乎与上次保存的xib状态一致,我的意思是:

  • 如果在将xib视为IPad时将其保存,我的tableview将在IPad上正确显示,但在iPhone上不会显示
  • 如果在将xib作为iPhone查看时将其保存,我的tableView将在iPhone上正确显示,但不会在iPad上显示
但是,如果我向下滚动以隐藏单元格并向上滚动,布局将变为OK。 希望我说得够清楚。。我错过了什么

在cellForItem中尝试LayoutFneed()和LayoutSubView()

可能将高度设置为800,并使用边距左上右左0?

尝试在cellForItem中使用LayoutFneed()和LayoutSubView()

可能将高度设置为800,并使用边距左上右上左0?

在xib中使用表格视图单元格时,特征变量应该可以正常工作

但是,如果你不经常使用它们,可能会让人困惑

试试这个

  • 创建一个空的xib
  • 添加表视图单元格
  • 添加标签(使用背景色查看)
将标签约束到顶部:0/前导:0/底部:0处的边距,并将其宽度约束为160:

现在,选择宽度约束,并在“尺寸检查器”窗格中,单击常量左侧的
+
符号,然后选择
宽度:常规
高度:任意

。。。然后单击添加变体:

现在将新性状变异的
常量
值更改为
600

以下是它在iPhone 8上的外观:

在iPad Pro 9.7上:

为了使用它,我使用了以下代码:

class TraitCell: UITableViewCell {
    @IBOutlet var theLabel: UILabel!
}

class TraitTestTableViewController: UITableViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(UINib(nibName: "TraitCell", bundle: nil), forCellReuseIdentifier: "traitCell")
    }
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let c = tableView.dequeueReusableCell(withIdentifier: "traitCell", for: indexPath) as! TraitCell
        c.theLabel.text = "\(indexPath)"
        return c
    }
}
下面是TraitCell.xib的源代码:

<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="17506" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
    <device id="retina4_7" orientation="portrait" appearance="light"/>
    <dependencies>
        <deployment identifier="iOS"/>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17505"/>
        <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
    </dependencies>
    <objects>
        <placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
        <placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
        <tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" rowHeight="48" id="Lnk-lV-Xmf" customClass="TraitCell" customModule="DelMe" customModuleProvider="target">
            <rect key="frame" x="0.0" y="0.0" width="414" height="48"/>
            <autoresizingMask key="autoresizingMask"/>
            <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="Lnk-lV-Xmf" id="A7R-fE-SaJ">
                <rect key="frame" x="0.0" y="0.0" width="414" height="48"/>
                <autoresizingMask key="autoresizingMask"/>
                <subviews>
                    <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="vB6-W5-Xl5">
                        <rect key="frame" x="16" y="11" width="160" height="26"/>
                        <color key="backgroundColor" red="0.98977929350000005" green="0.91474151609999999" blue="0.16292566059999999" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                        <constraints>
                            <constraint firstAttribute="width" constant="160" id="9U8-ws-UNn">
                                <variation key="widthClass=regular" constant="600"/>
                            </constraint>
                        </constraints>
                        <fontDescription key="fontDescription" type="system" pointSize="17"/>
                        <nil key="textColor"/>
                        <nil key="highlightedColor"/>
                    </label>
                </subviews>
                <constraints>
                    <constraint firstItem="vB6-W5-Xl5" firstAttribute="top" secondItem="A7R-fE-SaJ" secondAttribute="topMargin" id="5GP-0t-6Zx"/>
                    <constraint firstItem="vB6-W5-Xl5" firstAttribute="leading" secondItem="A7R-fE-SaJ" secondAttribute="leadingMargin" id="REV-Ua-5gO"/>
                    <constraint firstAttribute="bottomMargin" secondItem="vB6-W5-Xl5" secondAttribute="bottom" id="cF3-NH-NX1"/>
                </constraints>
            </tableViewCellContentView>
            <connections>
                <outlet property="theLabel" destination="vB6-W5-Xl5" id="G7V-WZ-215"/>
            </connections>
            <point key="canvasLocation" x="344" y="185.30734632683661"/>
        </tableViewCell>
    </objects>
</document>

在xib中使用表格视图单元格时,性状变异应该可以正常工作

但是,如果你不经常使用它们,可能会让人困惑

试试这个

  • 创建一个空的xib
  • 添加表视图单元格
  • 添加标签(使用背景色查看)
将标签约束到顶部:0/前导:0/底部:0处的边距,并将其宽度约束为160:

现在,选择宽度约束,并在“尺寸检查器”窗格中,单击常量左侧的
+
符号,然后选择
宽度:常规
高度:任意

。。。然后单击添加变体:

现在将新性状变异的
常量
值更改为
600

以下是它在iPhone 8上的外观:

在iPad Pro 9.7上:

为了使用它,我使用了以下代码:

class TraitCell: UITableViewCell {
    @IBOutlet var theLabel: UILabel!
}

class TraitTestTableViewController: UITableViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(UINib(nibName: "TraitCell", bundle: nil), forCellReuseIdentifier: "traitCell")
    }
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let c = tableView.dequeueReusableCell(withIdentifier: "traitCell", for: indexPath) as! TraitCell
        c.theLabel.text = "\(indexPath)"
        return c
    }
}
下面是TraitCell.xib的源代码:

<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="17506" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
    <device id="retina4_7" orientation="portrait" appearance="light"/>
    <dependencies>
        <deployment identifier="iOS"/>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17505"/>
        <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
    </dependencies>
    <objects>
        <placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
        <placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
        <tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" rowHeight="48" id="Lnk-lV-Xmf" customClass="TraitCell" customModule="DelMe" customModuleProvider="target">
            <rect key="frame" x="0.0" y="0.0" width="414" height="48"/>
            <autoresizingMask key="autoresizingMask"/>
            <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="Lnk-lV-Xmf" id="A7R-fE-SaJ">
                <rect key="frame" x="0.0" y="0.0" width="414" height="48"/>
                <autoresizingMask key="autoresizingMask"/>
                <subviews>
                    <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="vB6-W5-Xl5">
                        <rect key="frame" x="16" y="11" width="160" height="26"/>
                        <color key="backgroundColor" red="0.98977929350000005" green="0.91474151609999999" blue="0.16292566059999999" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                        <constraints>
                            <constraint firstAttribute="width" constant="160" id="9U8-ws-UNn">
                                <variation key="widthClass=regular" constant="600"/>
                            </constraint>
                        </constraints>
                        <fontDescription key="fontDescription" type="system" pointSize="17"/>
                        <nil key="textColor"/>
                        <nil key="highlightedColor"/>
                    </label>
                </subviews>
                <constraints>
                    <constraint firstItem="vB6-W5-Xl5" firstAttribute="top" secondItem="A7R-fE-SaJ" secondAttribute="topMargin" id="5GP-0t-6Zx"/>
                    <constraint firstItem="vB6-W5-Xl5" firstAttribute="leading" secondItem="A7R-fE-SaJ" secondAttribute="leadingMargin" id="REV-Ua-5gO"/>
                    <constraint firstAttribute="bottomMargin" secondItem="vB6-W5-Xl5" secondAttribute="bottom" id="cF3-NH-NX1"/>
                </constraints>
            </tableViewCellContentView>
            <connections>
                <outlet property="theLabel" destination="vB6-W5-Xl5" id="G7V-WZ-215"/>
            </connections>
            <point key="canvasLocation" x="344" y="185.30734632683661"/>
        </tableViewCell>
    </objects>
</document>


试着在你的
cellForItem
中调用
layoutifneed
。不起作用,我也在WillDisplaycell中尝试了。是的,我很害怕。也许你只是不能使用这个故事板功能;您必须自己在代码中进行“不同”的布局。我在表格视图和集合视图单元格进行自动布局时遇到了各种各样的问题。正如你的问题所暗示的,这里似乎存在计时问题。请尝试在你的
cellForItem
中调用
layoutifneedd
。不起作用,我在WillDisplayCelly中也尝试过。是的,我很害怕。也许你只是不能使用这个故事板功能;您必须自己在代码中进行“不同”的布局。我在表格视图和集合视图单元格进行自动布局时遇到了各种各样的问题。正如你的问题所暗示的,这里似乎有时间问题。这不起作用这不起作用