自定义UITableViewCell上没有图像

时间:2015-06-07 01:13:14

标签: ios uitableview swift

我正在尝试制作一个包含图像的自定义UITableViewCell。当用户向上/向下滚动表格时 - 这里会出现对表格单元格的视差效果。

所以,这是我的代码:

VEParallaxCell.swift:

import UIKit

class VEParallaxCell: UITableViewCell {

var parentTableView : UITableView?

var parallaxImage : UIImageView?

var parallaxRatio : CGFloat?

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

    super.init(style: style, reuseIdentifier: reuseIdentifier)

    self.contentView.backgroundColor = UIColor.whiteColor()

    parallaxImage = UIImageView.new()
    self.contentView.addSubview(parallaxImage!)

    parallaxImage?.backgroundColor = UIColor.whiteColor()
    parallaxImage?.contentMode = UIViewContentMode.ScaleAspectFill

    self.clipsToBounds = true

    parallaxRatio = 1.5



}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override func willMoveToSuperview(newSuperview: UIView?) {

    super.willMoveToSuperview(newSuperview)

    var view : UIView = newSuperview!

    if view.isKindOfClass(UITableView) {

        parentTableView = view as? UITableView


    }

    view = view.superview!

    if (parentTableView != nil) {

        parentTableView?.addObserver(self, forKeyPath: "contentOffset", options: NSKeyValueObservingOptions.New | NSKeyValueObservingOptions.Old, context: nil)

    }


}

override func removeFromSuperview() {

    super.removeFromSuperview()

    parentTableView?.removeObserver(self, forKeyPath: "contentOffset", context: nil)

    parentTableView = nil

}

override func layoutSubviews() {

    super.layoutSubviews()

    return

}



func setParallaxRatio(_parallaxRatio: CGFloat) {

    parallaxRatio = max(_parallaxRatio, 1.0)
    parallaxRatio = min(_parallaxRatio, 2.0)

    var rect : CGRect = self.bounds
    rect.size.height = rect.size.height * parallaxRatio!
    parallaxImage?.frame = rect

    updateParallaxOffset()

}

override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {

    if keyPath == "contentOffset" {

        var array = [VEParallaxCell]()

        array = parentTableView?.visibleCells() as! [VEParallaxCell]

        if parallaxRatio == 1.0 || !contains(array, self) {

            return

        }

        updateParallaxOffset()

    }

}

func updateParallaxOffset() {

    var contentOffset : CGFloat = parentTableView!.contentOffset.y
    var cellOffset : CGFloat = self.frame.origin.y - contentOffset

    var percent : CGFloat = (cellOffset + self.frame.size.height) / (parentTableView!.frame.size.height + self.frame.size.height)

    var extraHeight : CGFloat = self.frame.size.height * (parallaxRatio! - 1.0)

    var rect : CGRect? = parallaxImage?.frame

    rect!.origin.y = -extraHeight * percent

    parallaxImage?.frame = rect!


}


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
}

}

主视图控制器的单元格创建方法:

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

    var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! VEParallaxCell
    cell.parallaxImage?.image = UIImage(named: "eye.png")

    return cell
}

所以,问题是,当我构建并运行应用程序时,编译器没有显示任何错误和警告,但是当我向表中添加一个新单元格时,它会添加它,但不会显示任何图像。

我该怎么办?

1 个答案:

答案 0 :(得分:0)

您的图片视图的框架可能是(0, 0; 0, 0)(即CGRectZero),因此无法看到。你应该像这样创建它:

parallaxImage = UIImageView( frame: self.bounds )

此外,您的parallaxImageparallaxRatio属性不一定是选项,因为您是在init方法中定义的。

要获得tableView:cellForRowAtIndexPath中正在寻找的单元格,您应该进行有条件的向下转换:

if let cell = tableView.dequeueReusableCellWithIdentifier( "Cell", forIndexPath: indexPath) as? VEParallaxCell {
    cell.parallaxImage.image = UIImage(named: "eye.png")
    return cell
}
fatalError( "Unable to load cell of expected type 'VEParallaxCell'" )
相关问题