UITableViewCell变量ni?

时间:2015-08-31 05:50:24

标签: xcode swift

我使用笔尖正确设置了我的tableview。我注册了笔尖和标识符。不知何故,我的代码在这里得到一个零:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = galleryTable.dequeueReusableCellWithIdentifier("GalleryCustomCell", forIndexPath: indexPath) as! GalleryCell

然后它转到我创建的自定义单元格并告诉我每个变量都是零。我重新连接了我的网点,但仍然收到错误。

在我的自定义单元格中,它崩溃了:

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    self.backgroundColor = UIColor.whiteColor()

    var tapPhotoAction = UITapGestureRecognizer(target: self, action: Selector("photoTap:"))
    tapPhotoAction.delegate = self
    tapPhotoAction.cancelsTouchesInView = false
    galleryPhoto.addGestureRecognizer(tapPhotoAction)

有关为何会发生这种情况的任何想法?这是说galleryPhoto是零,但它不应该。我连接了插座。此外,这只发生在模拟器上,而不是在真实设备上。

编辑:

我没有使用故事板,只是一个xib。我有以下代码,所以我没有使用registerClass。

galleryCellLoader = UINib(nibName: "GalleryCell", bundle: nil) 
galleryTable.registerNib(galleryCellLoader, forCellReuseIdentifier: "GalleryCustomCell") 

3 个答案:

答案 0 :(得分:0)

尝试删除

self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "GalleryCustomCell")

然后从storyboard + ctrl中单击tableView,并将该行拖动到视图控制器并注册表视图。比视图控制器应该显示

 @IBOutlet weak var tableView: UITableView!

(你应该使用

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource  

在具有表视图的视图控制器中,单击storyboard + ctrl中的tableView并将其拖动到视图控制器,然后单击委托和数据源)

然后尝试再次运行代码。

答案 1 :(得分:0)

这可以解决您的问题。

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    // PART 1 BEGIN
    let identifier = "GalleryCustomCell"
    var cell:GalleryCell! = tableView.dequeueReusableCellWithIdentifier(identifier) as? GalleryCell
    if (cell == nil){
        let nib: NSArray = NSBundle.mainBundle().loadNibNamed("GalleryCell", owner: self.galleryTable, options: nil)
        cell = nib.objectAtIndex(0) as! GalleryCell
    }
    // PART 1 END

    // PART 2 BEGIN
    // if it is required, need to assign cell's delegate
    var tapPhotoAction = UITapGestureRecognizer(target: self, action: Selector("photoTap:"))
    //tapPhotoAction.delegate = self 
    tapPhotoAction.cancelsTouchesInView = false
    cell.galleryPhoto.addGestureRecognizer(tapPhotoAction)
    // PART 2 END

    return cell
}

仅使用第一部分,它不能解决您的问题。使用整个代码块。你也应该照顾细胞的代表。

答案 2 :(得分:0)

I have searched on the web for awhile now and finally found an answer to fix this problem. I did the following:

iPhone Simulator -> Reset Content and Settings...

Amazing!