Check if a variable is from subclass UICollectionViewCell
I have it UICollectionViewcell
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! PostCell
if let CurrentPost = posts[indexPath.row] as? Post {
if(CurrentPost.PostImage == nil) {
print(CurrentPost.PostText)
}
}
return cell
}
class PostCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
designCell()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
let postImage: UIImageView = {
let v = UIImageView()
v.translatesAutoresizingMaskIntoConstraints = false
v.contentMode = .scaleAspectFill
return v
}()
func designCell() {
addSubview(postImage)
if (postImage.image == nil) {
print("ss")
}
cellConstraints()
}
}
Now what I want to do is check if the posts[indexPath.row]
value PostImage
is zero or not. If it is nil, then in PostCell
I want to print "ss", otherwise I want to set the PostImage
image to PostImage
.
+3
source to share
3 answers
class PostCell: UICollectionViewCell {
var postImg: UIImage!
override init(frame: CGRect) {
super.init(frame: frame)
designCell()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
convenience init(frame: CGRect, img: UIImage) {
self.postImg = img
designCell()
}
let postImage: UIImageView = {
let v = UIImageView()
v.translatesAutoresizingMaskIntoConstraints = false
v.contentMode = .scaleAspectFill
v.image = postImg
return v
}()
func getImage(img: UIImage) {
self.postImg = img
}
func designCell(){
addSubview(postImage)
if(postImage.image == nil){
print("ss")
}
cellConstraints()
}
}
+1
source to share
First, create two different methods for setting image accessibility constraints.
class PostCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
designCell()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
let postImage: UIImageView = {
let v = UIImageView()
v.translatesAutoresizingMaskIntoConstraints = false
v.contentMode = .scaleAspectFill
return v
}()
func designCell(){
addSubview(postImage)
if(postImage.image == nil){
print("ss")
}
}
func cellConstraintForImageWithoutText(){
//Change your constraint if data have image but doesn't have text
}
func cellConstraintForImageWithText(){
//Change your constraint if data have image as well as text
}
func cellConstraintForNoImageWithoutText(){
//Change your constraint if data neither have image nor have text
}
func cellConstraintForNoImageWithText(){
//Change your constraint if data doesn't have image but have text
}
}
Now just check if the image is nil, then calling the NoImage else method calls another method.
let postText = CurrentPost.text
if let img = CurrentPost.PostImage {
if txt = postText {
cell.cellConstraintForImageWithText()
}else {
cell.cellConstraintForImageWithoutText()
}
}
else {
if txt = postText {
cell.cellConstraintForNoImageWithText()
}else {
cell.cellConstraintForNoImageWithoutText()
}
}
+1
source to share