ios - 多个自定义 UITableViewCell

标签 ios swift uitableview

尝试创建一个包含显示文本和照片以及仅显示文本的单元格的表格 View 。我创建了 2 个自定义 tableviewcell,但当我希望它们在 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?
}

最佳答案

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

那么你也许可以执行以下操作?一旦内容被解析,可能会重新加载表格 View 。

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
}

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

相关文章:

ios - Uitableview Cells 不能全部工作吗?

ios - PJSIP TLS 不适用于 iPhone

ios - NSDateComponentsFormatter 的方法 stringFromDate :ToDate: is returning a different value every second

iphone - UITableView的后台view怎么写(主要是Instructions)

ios - 插入新单元格时防止 UITableView 滚动

arrays - 通过抽取或提取 Swift 中的每个第 n 个元素进行下采样收集的有效方法

iphone - 如何处理被再次调用的类方法?

objective-c - 重新加载 TableView 的数据不起作用

ios - 我如何引用以编程方式添加文本字段以从 Swift 内部获取数据?

swift - Xcode:编译的应用程序文件无法像调试运行时一样工作