多个自定义UITableViewCells

时间:2016-05-26 00:26:26

标签: ios swift uitableview

尝试使用显示文本和照片以及文本的单元格创建tableview。我已经创建了2个自定义tableviewcells,但我只能让其中一个出现在我希望它们在tableview中混合的时候出现。基本上,如果帖子没有图像,我希望它加载TextFeedCell,如果是的话,我希望它加载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
            }
    }

感谢任何帮助。

更新1:

两者都已注册:

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

当我在没有条件语句的情况下实现它们时,这两个自定义单元都可以自行工作。问题来自于我使用条件实现它们(如上所述),我得到的帖子没有图像,但仍然会将空的imageView插入其中。

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)


    }
}

更新2:

发布模型

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

1 个答案:

答案 0 :(得分:0)

您没有检查和实际发布。您正在创建一个实际上没有帖子值的单元格实例。

所以你可以这样吗?并且可能在解析内容后重新加载tableview。

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
}