Michael Williams Michael Williams - 6 months ago 11
iOS Question

Multiple Custom UITableViewCells

Trying to create a tableview with cells that display text and photos and just text. I've created 2 custom tableviewcells but I am only able to get one of them to show up at a time when I want them to be mixed in the tableview. Basically, if the post has no image, I want it to load the TextFeedCell, if it does, I want it to load the photoFeedCell.

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

if FeedTVCellWithPhoto().post?.imageContentName != "" {
let photoFeedCell = tableView.dequeueReusableCellWithIdentifier(FeedTVController().cellId, forIndexPath: indexPath) as! FeedTVCellWithPhoto
photoFeedCell.post = posts[indexPath.section]
return photoFeedCell
}
else {
let textFeedCell = tableView.dequeueReusableCellWithIdentifier(FeedTVController().textCellId, forIndexPath: indexPath) as! FeedTVCellText
textFeedCell.post = posts[indexPath.section]
return textFeedCell
}
}


Any help is appreciated.

Update 1:

Both are registered:

tableView?.registerClass(FeedTVCellWithPhoto.self, forCellReuseIdentifier: cellId)
tableView?.registerClass(FeedTVCellText.self, forCellReuseIdentifier: textCellId)


Both of these custom cells work on their own, when I implement them without the conditional statement. The problem comes when I implement them with the conditional(as-is above), I get the posts that don't have images but still get empty imageViews inserted into them.

FeedTVCellWithPhoto

class FeedTVCellWithPhoto: UITableViewCell {

var post:Post? {
didSet {
if let name = post?.name {

let attributedText = NSMutableAttributedString(string: name, attributes: [NSFontAttributeName:UIFont.boldSystemFontOfSize(14)])

attributedText.appendAttributedString(NSAttributedString(string: "\nMay 23 • Los Angeles, CA • ", attributes: [NSFontAttributeName:UIFont.systemFontOfSize(10), NSForegroundColorAttributeName:UIColor.lightGrayColor()]))

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 5

attributedText.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, attributedText.string.characters.count))

let attachment = NSTextAttachment()
attachment.image = UIImage(named: "globe")
attachment.bounds = CGRectMake(0, -2, 12, 12)
attributedText.appendAttributedString(NSAttributedString(attachment: attachment))

nameLabel.attributedText = attributedText
}
if let textContent = post?.textContent {
postTextView.text = textContent
}
if let profileImageName = post?.profileImageName {
profileImg.image = UIImage(named: profileImageName)
}
if let imageContent = post?.imageContentName {
print(imageContent)
postImageView.image = UIImage(named: imageContent)
}
}
}

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: .Default, reuseIdentifier: FeedTVController().cellId)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

let nameLabel:UILabel = {
let label = UILabel()
label.numberOfLines = 2
return label
}()

let profileImg:UIImageView = {
let imageView = UIImageView()
imageView.image = UIImage(named: "profile")
imageView.contentMode = .ScaleAspectFit
return imageView
}()

let postTextView:UITextView = {
let textView = UITextView()
textView.text = "This is text from a post!"
textView.font = UIFont.systemFontOfSize(14)
textView.scrollEnabled = false
textView.editable = false
return textView
}()

let postImageView:UIImageView = {
let imageView = UIImageView()
imageView.contentMode = .ScaleAspectFill
imageView.layer.masksToBounds = true
return imageView

}()

let likesCommentsLabel:UILabel = {
let label = UILabel()
label.text = "25 Likes 10.7K Comments"
label.font = UIFont.systemFontOfSize(12)
label.textColor = UIColor.rgb(155, green: 161, blue: 171)
return label
}()

let dividerLineView:UIView = {
let view = UIView()
view.backgroundColor = UIColor.rgb(226, green: 228, blue: 232)
return view
}()

let likeButton = FeedTVCellWithPhoto.buttonForTitle("Like", imageName: "like")
let commentButton = FeedTVCellWithPhoto.buttonForTitle("Comment", imageName: "comment")
let shareButton = FeedTVCellWithPhoto.buttonForTitle("Share", imageName: "share")

static func buttonForTitle(title:String, imageName:String) -> UIButton {
let button = UIButton()
button.setTitle(title, forState: .Normal)
button.setTitleColor(UIColor.rgb(143, green: 150, blue: 163), forState: .Normal)
button.setTitleColor(.blueColor(), forState: .Highlighted)
button.setTitleColor(.redColor() , forState: .Selected)
button.setImage(UIImage(named:imageName), forState: .Normal)
button.titleEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 0)
button.titleLabel!.font = UIFont.boldSystemFontOfSize(14)
return button
}

func setupViews() {
backgroundColor = .whiteColor()
addSubview(nameLabel)
addSubview(profileImg)
addSubview(postTextView)
addSubview(postImageView)
addSubview(likesCommentsLabel)
addSubview(dividerLineView)
addSubview(likeButton)
addSubview(commentButton)
addSubview(shareButton)
addContstraintsWithFormat("H:|-8-[v0(44)]-8-[v1]|", views: profileImg, nameLabel)
addContstraintsWithFormat("V:|-8-[v0]", views: nameLabel)
addContstraintsWithFormat("H:|-4-[v0]-4-|", views: postTextView)
addContstraintsWithFormat("H:|-12-[v0]-12-|", views: dividerLineView)
addContstraintsWithFormat("H:|-12-[v0]|", views: likesCommentsLabel)
addContstraintsWithFormat("H:|[v0(v2)][v1(v2)][v2]|", views: likeButton, commentButton, shareButton)
addContstraintsWithFormat("V:[v0(44)]|", views: commentButton)
addContstraintsWithFormat("V:[v0(44)]|", views: shareButton)
addContstraintsWithFormat("V:|-8-[v0(44)]-4-[v1]-4-[v2(200)][v3(44)][v4(1)][v5(44)]|", views: profileImg, postTextView, postImageView, likesCommentsLabel, dividerLineView, likeButton)
addContstraintsWithFormat("H:|[v0]|", views: postImageView)


}
}


FeedTVCellText

class FeedTVCellText: UITableViewCell {
var post:Post? {
didSet {
if let name = post?.name {

let attributedText = NSMutableAttributedString(string: name, attributes: [NSFontAttributeName:UIFont.boldSystemFontOfSize(14)])

attributedText.appendAttributedString(NSAttributedString(string: "\nMay 23 • Los Angeles, CA • ", attributes: [NSFontAttributeName:UIFont.systemFontOfSize(10), NSForegroundColorAttributeName:UIColor.lightGrayColor()]))

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 5

attributedText.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, attributedText.string.characters.count))

let attachment = NSTextAttachment()
attachment.image = UIImage(named: "globe")
attachment.bounds = CGRectMake(0, -2, 12, 12)
attributedText.appendAttributedString(NSAttributedString(attachment: attachment))

nameLabel.attributedText = attributedText
}
if let textContent = post?.textContent {
postTextView.text = textContent
}
if let profileImageName = post?.profileImageName {
profileImg.image = UIImage(named: profileImageName)
}

}
}

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: .Default, reuseIdentifier: FeedTVController().textCellId)
setupViews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

let nameLabel:UILabel = {
let label = UILabel()
label.numberOfLines = 2
return label
}()

let profileImg:UIImageView = {
let imageView = UIImageView()
imageView.image = UIImage(named: "profile")
imageView.contentMode = .ScaleAspectFit
return imageView
}()

let postTextView:UITextView = {
let textView = UITextView()
textView.text = "This is text from a post!"
textView.font = UIFont.systemFontOfSize(14)
textView.scrollEnabled = false
textView.editable = false
return textView
}()

let likesCommentsLabel:UILabel = {
let label = UILabel()
label.text = "25 Likes 10.7K Comments"
label.font = UIFont.systemFontOfSize(12)
label.textColor = UIColor.rgb(155, green: 161, blue: 171)
return label
}()

let dividerLineView:UIView = {
let view = UIView()
view.backgroundColor = UIColor.rgb(226, green: 228, blue: 232)
return view
}()

let likeButton = FeedTVCellText.buttonForTitle("Like", imageName: "like")
let commentButton = FeedTVCellText.buttonForTitle("Comment", imageName: "comment")
let shareButton = FeedTVCellText.buttonForTitle("Share", imageName: "share")

static func buttonForTitle(title:String, imageName:String) -> UIButton {
let button = UIButton()
button.setTitle(title, forState: .Normal)
button.setTitleColor(UIColor.rgb(143, green: 150, blue: 163), forState: .Normal)
button.setTitleColor(.blueColor(), forState: .Highlighted)
button.setTitleColor(.redColor() , forState: .Selected)
button.setImage(UIImage(named:imageName), forState: .Normal)
button.titleEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 0)
button.titleLabel!.font = UIFont.boldSystemFontOfSize(14)
return button
}

func setupViews() {
backgroundColor = .whiteColor()
addSubview(nameLabel)
addSubview(profileImg)
addSubview(postTextView)
addSubview(likesCommentsLabel)
addSubview(dividerLineView)
addSubview(likeButton)
addSubview(commentButton)
addSubview(shareButton)
addContstraintsWithFormat("H:|-8-[v0(44)]-8-[v1]|", views: profileImg, nameLabel)
addContstraintsWithFormat("V:|-8-[v0]", views: nameLabel)
addContstraintsWithFormat("H:|-4-[v0]-4-|", views: postTextView)
addContstraintsWithFormat("H:|-12-[v0]-12-|", views: dividerLineView)
addContstraintsWithFormat("H:|-12-[v0]|", views: likesCommentsLabel)
addContstraintsWithFormat("H:|[v0(v2)][v1(v2)][v2]|", views: likeButton, commentButton, shareButton)
addContstraintsWithFormat("V:[v0(44)]|", views: commentButton)
addContstraintsWithFormat("V:[v0(44)]|", views: shareButton)
addContstraintsWithFormat("V:|-8-[v0(44)]-4-[v1]-4-[v2(44)][v3(1)][v4(44)]|", views: profileImg, postTextView, likesCommentsLabel, dividerLineView, likeButton)


}
}


Update 2:

Post model

class Post {
var name:String?
var profileImageName:String?
var textContent:String?
var imageContentName:String?
}

Answer

You are not checking against and actual post. You are creating an instance of a cell that doesn't actually have a post value.

So you can maybe the following? And possibly reload the tableview once the content is parsed.

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

    var post : Post? = posts[indexPath.section]

    if let _ = post?.imageContentName {
        let photoFeedCell = tableView.dequeueReusableCellWithIdentifier(FeedTVController().cellId, forIndexPath: indexPath) as? FeedTVCellWithPhoto
        photoFeedCell.post = post?
        return photoFeedCell
    } 

    let textFeedCell = tableView.dequeueReusableCellWithIdentifier(FeedTVController().textCellId, forIndexPath: indexPath) as? FeedTVCellText
    textFeedCell.post = post?
    return textFeedCell
}