通过故事板添加的tableViewCell中的UIElements的零接收

问题描述:

我已经完全在故事板中创建了一个TableViewCell,就像我在任何其他应用程序中一样。我想设置属性,但其内部的组件(如cellScrollView)正在返回。他们通过IBOutlet连接。我从未有过这种情况发生前对我来说,其中发生错误行:通过故事板添加的tableViewCell中的UIElements的零接收

cell.cellScrollView.contentSize = CGSizeMake((2 * cell.frame.size.width), cell.frame.size.height) 

和错误是:

fatal error: unexpectedly found nil while unwrapping an Optional value

下面是视图控制器..

import UIKit 

class ViewController: UIViewController, UITableViewDataSource { 
    @IBOutlet weak var scrollView: UIScrollView! 

    func random() -> CGFloat { return CGFloat(rand())/CGFloat(RAND_MAX) } 
    func randomColor() -> UIColor { return UIColor(red: random(), green: random(), blue: random(), alpha: 1.0) } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 3, self.view.frame.height) 
     scrollView.contentOffset = CGPoint(x: self.view.frame.size.width, y: 0) 

     var tableView = UITableView(frame: CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)) 
     tableView.dataSource = self 
     tableView.registerClass(SnapchatTableViewCell.self, forCellReuseIdentifier: "cell") 
     tableView.separatorStyle = UITableViewCellSeparatorStyle.None 

     scrollView.addSubview(tableView) 

     // Do any additional setup after loading the view, typically from a nib. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    //TableView DataSource 

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as SnapchatTableViewCell 
     cell.delegate = self 
     cell.selectionStyle = UITableViewCellSelectionStyle.None 

     cell.cellScrollView.contentSize = CGSizeMake((2 * cell.frame.size.width), cell.frame.size.height) //This is where the crash occurs, the error tells me cell.cellScrollView is nil 

     return cell 
    } 


} 

..这里是继承UITableViewCell的类,它是故事板中单元格的自定义类:

import UIKit 

class SnapchatTableViewCell: UITableViewCell, UIScrollViewDelegate { 
    @IBOutlet var cellScrollView: UIScrollView! 
    @IBOutlet var colorView: UIView! 

    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 
    } 

    //ScrollViewDelegate 

    func scrollViewDidScroll(scrollView: UIScrollView) { 

    } 

} 

在故事板中使用viewWithTag()功能而不是使用插座。

给你的cellScrollView一个标签,例如100,然后简单地调用cell.viewWithTag(100) as UIScrollView来访问你的滚动视图。对您单元格中的所有子视图执行相同操作。

+0

这是为什么?为什么我不能在这种情况下使用IBOutlets? – CaptainCOOLGUY 2014-11-04 04:12:00

+0

我不确定...也许有人可以对此有所了解?对于内容标签应该保留在每个实例中的单元格的重用可能有一些问题,因为IB单元格只是一个原型,所以网点并不与每个实例连接。但我不确定... – Bennie 2014-11-08 19:04:53

+0

不幸的是,它并没有解决我的问题(对象仍然是零),谢谢 – CaptainCOOLGUY 2014-11-14 22:52:34