PostTableViewCell.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // TweetTableViewCell.m
  2. //
  3. // Copyright (c) 2012 Mattt Thompson (http://mattt.me/)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. #import "PostTableViewCell.h"
  23. #import "Post.h"
  24. #import "User.h"
  25. #import "UIImageView+AFNetworking.h"
  26. @implementation PostTableViewCell
  27. - (id)initWithStyle:(UITableViewCellStyle)style
  28. reuseIdentifier:(NSString *)reuseIdentifier
  29. {
  30. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  31. if (!self) {
  32. return nil;
  33. }
  34. self.textLabel.adjustsFontSizeToFitWidth = YES;
  35. self.textLabel.textColor = [UIColor darkGrayColor];
  36. self.detailTextLabel.font = [UIFont systemFontOfSize:12.0f];
  37. self.detailTextLabel.numberOfLines = 0;
  38. self.selectionStyle = UITableViewCellSelectionStyleGray;
  39. return self;
  40. }
  41. - (void)setPost:(Post *)post {
  42. _post = post;
  43. self.textLabel.text = _post.user.username;
  44. self.detailTextLabel.text = _post.text;
  45. [self.imageView setImageWithURL:_post.user.avatarImageURL placeholderImage:[UIImage imageNamed:@"profile-image-placeholder"]];
  46. [self setNeedsLayout];
  47. }
  48. + (CGFloat)heightForCellWithPost:(Post *)post {
  49. #pragma clang diagnostic push
  50. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  51. CGSize sizeToFit = [post.text sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:CGSizeMake(220.0f, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];
  52. #pragma clang diagnostic pop
  53. return fmaxf(70.0f, sizeToFit.height + 45.0f);
  54. }
  55. #pragma mark - UIView
  56. - (void)layoutSubviews {
  57. [super layoutSubviews];
  58. self.imageView.frame = CGRectMake(10.0f, 10.0f, 50.0f, 50.0f);
  59. self.textLabel.frame = CGRectMake(70.0f, 10.0f, 240.0f, 20.0f);
  60. CGRect detailTextLabelFrame = CGRectOffset(self.textLabel.frame, 0.0f, 25.0f);
  61. detailTextLabelFrame.size.height = [[self class] heightForCellWithPost:_post] - 45.0f;
  62. self.detailTextLabel.frame = detailTextLabelFrame;
  63. }
  64. @end