无法在UITableViewCell内部识别UIScrollView,nil错误

时间:2016-01-19 02:31:13

标签: ios swift uitableview uiscrollview

因此,当我运行我的代码时,它会在awakeFromNib函数中出错。它似乎没有识别我从IBOutlet定义的UIScrollView。这是我的UITableViewCell代码:

import UIKit

class ItemDetailsCell: UITableViewCell, UIScrollViewDelegate {


    @IBOutlet weak var centerImage: UIImageView!

    @IBOutlet weak var labelBlack: UILabel!

    @IBOutlet weak var heading: UILabel!

    @IBOutlet weak var body: UILabel!

    @IBOutlet weak var tableScrollView: UIScrollView!


    var pageImages: [UIImage] = []
    var pageViews: [UIImageView?] = []

    static var parentView : ItemDetailsVC!;


    override func awakeFromNib() {
        super.awakeFromNib()

        //tableScrollView.delegate = self;
        var scrollViewWidth = ItemDetailsCell.parentView.scrollViewWidth
        var scrollViewHeight = ItemDetailsCell.parentView.scrollViewHeight

        if let tableCheck = ItemDetailsCell.parentView.scrollImages as [Dictionary<Int,UIImage>]? {
            if ItemDetailsCell.parentView.scrollImages.count > 0 {
                for var subviewCounter: Int = 0; subviewCounter <= tableCheck.count; subviewCounter++ {
                    for scrollImage in tableCheck {
                        if let imgToInsert = scrollImage[subviewCounter] as UIImage? {
                            let viewWidth = CGFloat(subviewCounter-1)
                            let imgTwo = UIImageView(frame: CGRectMake(scrollViewWidth * viewWidth, 0, scrollViewWidth, scrollViewHeight))
                            imgTwo.image = imgToInsert
                            imgTwo.contentMode = .ScaleAspectFit
                            imgTwo.userInteractionEnabled = true
                            var gesture = UITapGestureRecognizer(target: self, action: "tappedOnImage:")
                            var pinchGesture = UIPinchGestureRecognizer(target: self, action: "pinchedImage:")
                            imgTwo.addGestureRecognizer(gesture)
                            imgTwo.addGestureRecognizer(pinchGesture)
                            tableScrollView.addSubview(imgTwo)
                        } else {
                            println("no match")
                        }

                    }
                }

所以我在其他ViewController中有一个UITableView,它有2个原型单元格,一个是UIScrollView,另一个是文本标签。这是我的UITableView代码:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        if indexPath.row == 0 {

        let myCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! ItemDetailsCell

        myCell.centerImage.image = mainImage

            myCell.heading.text = detailsHeadings[indexPath.row]
            let headingString = detailsHeadings[indexPath.row]
            myCell.body.text = details[headingString]

        println("Label Height")
        println(myCell.labelBlack.frame.height)
        myCell.tableScrollView.frame = scrollViewOrigFrame

        return myCell

        } else {

我得到的错误是它加载的时候我收到的错误是“致命错误:在tableScrollView变量上解包一个Optional值时意外发现nil。”

知道为什么我会这样做吗?我可能不是在表格中做最好的滚动图像查看器的方法,但我找不到很多信息。

1 个答案:

答案 0 :(得分:0)

你可能会收到错误

    let myCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! ItemDetailsCell

它展开了可选值,但发现它为nil。是否在tableView上注册了标识为Cell的单元格?