2016-05-26 13 views
0

テキストと写真とテキストのみを表示するセルでテーブルビューを作成しようとしています。私は2つのカスタムtableviewcellsを作成しましたが、私はそれらを表ビューで混在させたいときに一度に表示することができます。基本的には、画像に画像がない場合は、TextFeedCellをロードして、そうであればphotoFeedCellをロードします。複数のカスタムUITableViewCells

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) 

私は条件文なしでそれらを実装する際にこれらのカスタムセルの両方が、自分自身に取り組みます。問題は、条件付きで(上記のように)それらを実装すると、イメージを持たないポストを取得しますが、まだ空のimageViewsが挿入されてしまいます。

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:

Postモデル

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

これはiOSでも問題ないので、間違ったセルIDやセクション/行数を入力したか、コードに論理エラーがあります。私は、あなたがストーリーボードと 'FeedTVController()'のセル識別子をチェックし、デバッガを使ってコードをチェックしてロジックをチェックすることをお勧めします。 –

+0

コードは正常です。あなたの変数の内容を再確認してください。 'post'。 Interface Builder/storyboardの識別子を確認してください。 – MirekE

+0

.xibファイルを使用している場合は、それらをテーブルビューに登録する必要があります。 –

答えて

0

あなたは実際の投稿と照合していません。実際にポスト値を持たないセルのインスタンスを作成しています。

次のようなことがありますか?そして、コンテンツが解析されたら、おそらくテーブルビューをリロードします。

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 
} 
+0

私はあなたのコードを追加しましたが、私はまだそれらのすべてのために(写真がない場合でも)写真付きのセルを取得しています。まだそのimageViewを追加していますが、そのセルにはありません –

+0

@MichaelWilliams私は自分の答えを更新しました。実際のポスト値を持たないセルのインスタンスを作成しているようです。試してみてください:) – AntonTheDev

+0

はまだ同じ問題を抱えています。投稿ごとにphotoFeedCellが返されます –

関連する問題