简体   繁体   English

带有tableview dequeueReusableCellWithIdentifier的零个出口

[英]Nil outlets with tableview dequeueReusableCellWithIdentifier

I've an ios project with a table view controller scene with custom tableView cells. 我有一个带有自定义tableView单元格的表视图控制器场景的ios项目。 When I try to set some value to an outlet in the cellForRowAtIndexPath method this error is thrown: 当我尝试为cellForRowAtIndexPath方法中的插座设置一些值时,会引发此错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

The nil value is the outlet reference of my custom tableView cell class. nil值是我的自定义tableView单元格类的出口引用。

I can't figure out what is causing this, since I think that I've configured all correctly. 我不知道是什么原因造成的,因为我认为我已经正确配置了所有配置。

This is the code I'm using: 这是我正在使用的代码:

MyTableViewController MyTableViewController

class MyTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.registerClass(MyTableViewCell.self, forCellReuseIdentifier: "myTableViewCell")
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("myTableViewCell", forIndexPath: indexPath) as! MyTableViewCell

        cell.myLabel?.text = "22:40"
        return cell
    }
}

MyTableViewCell MyTableViewCell

class MyTableViewCell: UITableViewCell {
    @IBOutlet var myLabel: UILabel!
}

Part of the source code of the Main.storyboard : Main.storyboard的部分源代码:

<scene sceneID="lJU-Ak-NqO">
    <objects>
        <tableViewController id="Uep-sz-gtX" customClass="MyTableViewController" customModule="My_App" customModuleProvider="target" sceneMemberID="viewController">
            <tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="28" sectionFooterHeight="28" id="Ld7-YH-V2O">
                <rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
                <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
                <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
                <prototypes>
                    <tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="myTableViewCell" id="Xd0-56-JUL" customClass="MyTableViewCell" customModule="My_App" customModuleProvider="target">
                        <rect key="frame" x="0.0" y="28" width="375" height="44"/>
                        <autoresizingMask key="autoresizingMask"/>
                        <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="Xd0-56-JUL" id="Yrq-Di-Pfr">
                            <rect key="frame" x="0.0" y="0.0" width="375" height="43"/>
                            <autoresizingMask key="autoresizingMask"/>
                            <subviews>
                                <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="og4-63-SRD">
                                    <rect key="frame" x="8" y="11" width="42" height="21"/>
                                    <fontDescription key="fontDescription" type="system" pointSize="17"/>
                                    <color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
                                    <nil key="highlightedColor"/>
                                </label>
                            </subviews>
                        </tableViewCellContentView>
                        <connections>
                            <outlet property="myLabel" destination="og4-63-SRD" id="YpX-Ia-DK7"/>
                        </connections>
                    </tableViewCell>
                </prototypes>
                <connections>
                    <outlet property="dataSource" destination="Uep-sz-gtX" id="8kh-UV-dfO"/>
                    <outlet property="delegate" destination="Uep-sz-gtX" id="xyW-cf-Nth"/>
                </connections>
            </tableView>
            <simulatedScreenMetrics key="simulatedDestinationMetrics" type="retina47"/>
        </tableViewController>
        <placeholder placeholderIdentifier="IBFirstResponder" id="Xja-Ye-Jqb" userLabel="First Responder" sceneMemberID="firstResponder"/>
    </objects>
    <point key="canvasLocation" x="401.5" y="-563.5"/>
</scene>

I've already checked the storyboard settings for the custom class controller and the custom table view cell class. 我已经检查了自定义类控制器和自定义表格视图单元格类的情节提要设置。 Even the cell identifier is correct in the xcode attributes inspector. 即使单元格标识符在xcode属性检查器中也是正确的。

Another strange behavior is that the label that I've added in my cell prototype isn't visible at all (it has a default test), even if I don't try to set the custom text. 另一个奇怪的行为是,即使我不尝试设置自定义文本,我在单元原型中添加的标签也完全不可见(它具有默认测试)。 Same behavior if I try to resize the prototype cell (it's show at it's default size). 如果我尝试调整原型单元的大小,则行为相同(显示为默认大小)。

UPDATE As suggested by @dfri, I've created a new example project from scratch and everything works. 更新正如@dfri所建议的那样,我从头开始创建了一个新的示例项目,一切正常。 The only difference between projects is that here I'm using SSASideMenu so I think that it is the cause of this problem. 项目之间的唯一区别是我在这里使用SSASideMenu,所以我认为这是导致此问题的原因。

The code I'm using to show the tableView when the menu item is tapped, is this ( as suggested here ): 轻按菜单项时用来显示tableView的代码是( 如此处建议的 ):

sideMenuViewController?.contentViewController = UINavigationController(rootViewController: (storyBoard.instantiateViewControllerWithIdentifier("MyTableViewController") as? MyTableViewController)!)
sideMenuViewController?.hideMenuViewController()

I'm using this code for other "plain" view but with the table view I've this problem. 我正在将此代码用于其他“普通”视图,但是在表视图中却遇到了这个问题。 Any advice is welcome. 欢迎任何建议。

EDIT2 ( after your addition that the actual problem wasn't the table view controller, as verified by your just recreating the project form scratch ) EDIT2( 在您添加了实际的问题不是表视图控制器之后,通过重新创建项目表单草稿进行了验证

Ok, try replacing the following line 好的,尝试替换以下行

sideMenuViewController?.contentViewController = UINavigationController(rootViewController: (storyBoard.instantiateViewControllerWithIdentifier("MyTableViewController") as? MyTableViewController)!)

with

sideMenuViewController?.contentViewController = UINavigationController(rootViewController: (storyBoard.instantiateViewControllerWithIdentifier("MyTableViewController") as! UITableViewController))

You know that you MyTableViewController (btw, are you sure you shouldn't use the myTableViewController identifier here? Case-sensitivity..) is a subclass of UITableViewController , so you can use the forced conversion as! 你知道你MyTableViewController (顺便说一句,你确定你不应该使用myTableViewController这里标识?区分大小写..)是的子类UITableViewController ,所以你可以使用强制转换as! to cast it to it's parent, UITableViewController . 将其转换为父级UITableViewController The UINavigationController(rootViewController: ... takes a UIViewController as argument, so you need to cast to something Xcode can assess to be a UIViewController object. UINavigationController(rootViewController: ...UIViewController作为参数,因此您需要转换为Xcode可以评估为UIViewController对象的对象。


EDIT ( after your addition of part of the source code of the Main.storyboard ) 编辑( 添加Main.storyboard的部分源代码之后

From the storyboard nib you just posted, we see that your reuseIdentifier for your custom class is "MyTableViewCell" ( reuseIdentifier="MyTableViewCell" ), not myTableViewCell (case-sensitivity), where the latter is what you use int your table view controller when trying to access the cell. 从笔尖您刚刚张贴的故事板,我们看到您的reuseIdentifier您的自定义类是"MyTableViewCell"reuseIdentifier="MyTableViewCell" ),而不是myTableViewCell (区分大小写),后者是你用什么INT你的表视图控制器时试图访问该单元。 So preferably update your reuse identifier (via attributes inspector) to myTableViewCell . 因此,最好将您的重用标识符(通过属性检查器)更新为myTableViewCell

Also, remove the line self.tableView.registerClass(MyTableViewCell.self, forCellReuseIdentifier: "myTableViewCell") from your viewDidLoad method; 另外,从viewDidLoad方法中删除以下行: self.tableView.registerClass(MyTableViewCell.self, forCellReuseIdentifier: "myTableViewCell") you don't need to explicitly register the class in this case (already made the connection to the custom cell class in your storyboard). 在这种情况下,您无需显式注册类(已经在情节提要中建立了与自定义单元格类的连接)。


Note also that the myLabel in your custom table view cell class ( MyTableViewCell ) isn't an optional, so you shouldn't include the ? 另请注意,自定义表格视图单元格类( MyTableViewCell )中的myLabel不是可选的,因此您不应包含? operator when accessing it. 操作员在访问它时。

Change the line cell.myLabel?.text = "22:40" to 将行cell.myLabel?.text = "22:40"更改为

cell.myLabel.text = "22:40"

If the above doesn't fix you error, have a look at my answer in this thread: 如果以上方法不能解决您的错误,请在此线程中查看我的答案:

It describes in detail just a case like yours, so you should be able to the answer as a "tutorial" to go over your code, step by step. 它仅详细描述了您的情况,因此您应该能够将答案作为“教程”逐步解决您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM