我正在尝试在Eureka中创建一个显示图像的自定义单元格。点按图像时,图像将以黑色背景显示全屏。
通常你用view.addSubview(newImageView)来做,但它似乎不像Eureka单元有视图类。
这是我到目前为止所获得的细胞:
final class ImageViewCell: Cell<ImageView>, CellType {
@IBOutlet weak var viewImage: UIImageView!
//let storage = Storage.storage().reference()
required init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func setup() {
super.setup()
//cell not selectable
selectionStyle = .none
viewImage.contentMode = .scaleAspectFill
viewImage.clipsToBounds = true
height = {return 300 }
//make userImage reconize tapping
let tapRec = UITapGestureRecognizer(target: self, action: #selector(imageTapHandler(tapGestureRecognizer:)))
viewImage.addGestureRecognizer(tapRec)
viewImage.isUserInteractionEnabled = true
}
@objc func imageTapHandler(tapGestureRecognizer: UITapGestureRecognizer) {
let imageView = tapGestureRecognizer.view as! UIImageView
let newImageView = UIImageView(image: imageView.image)
newImageView.frame = UIScreen.main.bounds
newImageView.backgroundColor = .black
newImageView.contentMode = .scaleAspectFit
newImageView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
newImageView.addGestureRecognizer(tap)
view.addSubview(newImageView)
}
@objc func dismissFullscreenImage(_ sender: UITapGestureRecognizer) {
sender.view?.removeFromSuperview()
}
override func update() {
super.update()
// we do not want to show the default UITableViewCell's textLabel
textLabel?.text = nil
// get the value from our row
guard let imageData = row.value else { return }
// get user image data from value
let downloadData = imageData.pictureData
viewImage.image = UIImage(data: downloadData)
}
}
我在尝试添加子视图时会收到“使用未解析的标识符'视图'。
我尝试使用contentView而不是view,结果是这样的:
视图的屏幕截图
答案 0 :(得分:0)
如果您想在fullScreen中显示图片,则单元格contentView
或包含此单元格的viewController
子视图不适合将此image
添加为{ {1}}。
正确的解决方法是在容器subview
中使用onCellSelection
(如下所示)回调,然后展示新的ViewController
,仅显示全屏图像或者你想要的任何定制。
ViewController
如果您只想在用户点按单元格中的图片时显示全屏,那么您应该在容器imageCell.onCellSelection({[weak self] (cell, row) in
let imageVC = DisplayViewController()
imageVC.image = cell.viewImage.image
self?.present(imageVC, animated: true, completion: nil)
})
中获取tapGesture
callback
并显示上图。
答案 1 :(得分:0)
感谢卡姆兰,我想我找到了解决方案。
对于Eureka行,您始终可以使用row.onCellSelection回叫。
就我而言,我可以在调用自定义行时执行此操作:
<<< ImageViewCellRow(){ row in
row.value = ImageView(pictureData: data!)
row.onCellSelection(showMe)
}
然后创建如下所示的showMe函数:
func showMe(cell: ImageViewCell, row: (ImageViewCellRow)) {
print("tapped my ImageViewCell!")
}
现在您应该可以将全屏图像作为Kamran的代码显示。