gpt4 book ai didi

ios - 自定义 UITableViewCell 上没有图像

转载 作者:行者123 更新时间:2023-11-30 14:19:19 24 4
gpt4 key购买 nike

我正在尝试制作一个包含图像的自定义 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
}

}

以及主视图 Controller 的单元格创建方法:

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
}

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

我应该做什么?

最佳答案

您的 ImageView 的框架可能是 (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'" )

关于ios - 自定义 UITableViewCell 上没有图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30689097/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com