iOS开发之新浪微博山寨版代码优化

 之前发表过一篇博客“IOS开发之新浪围脖”,在编写代码的时候太偏重功能的实现了,写完基本功能后看着代码有些别扭,特别是用到的四种cell的类,重复代码有点多,所以今天花点时间把代码重构一下。为了减少代码的重复编写把cell中相同的部分抽象成父类,然后继承。不过也是结合着storyboard做的。在优化时转发的View和评论的View相似,于是就做了个重用。在原来的代码上就把cell的代码进行了重写,所以本篇作为补充,关键代码还得看之前的博客。

  1.第一种cell,只有微博内容,没有图片,效果如下:

iOS开发之新浪微博山寨版代码优化

  cell对应的代码如下:

  TextTableViewCell.h

#import <UIKit/UIKit.h>

//TableView要回调的block,用于把cell中的按钮的tag传给TableView
typedef  void (^MyCellBlock) (UITableViewCell * cell, int tag);

@interface TextTableViewCell : UITableViewCell
//接收block块
-(void)setMyCellBlock:(MyCellBlock) block;

//接收字典
-(void) setDic:(NSDictionary *)dic;

@end

 TextTableViewCell.m(带图片的cell继承于这个cell)
#import "TextTableViewCell.h"

@interface TextTableViewCell()

@property (strong, nonatomic) IBOutlet UIImageView *headImage;
@property (strong, nonatomic) IBOutlet UILabel *nameLabel;
@property (strong, nonatomic) IBOutlet UILabel *dateLabel;
@property (strong, nonatomic) IBOutlet UILabel *weiboTextLabel;

@property (strong, nonatomic) NSDictionary *dic;
@property (strong, nonatomic) MyCellBlock block;

@end

@implementation TextTableViewCell

//获取传入的block块
-(void)setMyCellBlock:(MyCellBlock)block
{
    self.block = block;
}

//获取传入的参数,用于给我们的cell中的标签赋值
-(void) setDic:(NSDictionary *)dic
{
    
    //设置头像
   [self.headImage setImageWithURL:[NSURL URLWithString:dic[@"user"][@"profile_image_url"]]];
    
    //设置昵称
    self.nameLabel.text = dic[@"user"][@"name"];
    
    //设置时间
    NSDateFormatter *iosDateFormater=[[NSDateFormatter alloc]init];
    [email protected]"EEE MMM d HH:mm:ss Z yyyy";

    //必须设置,否则无法解析
    iosDateFormater.locale=[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];
   NSDate *date=[iosDateFormater dateFromString:dic[@"created_at"]];
  
     //目的格式
     NSDateFormatter *resultFormatter=[[NSDateFormatter alloc]init];
     [resultFormatter setDateFormat:@"MM月dd日 HH:mm"];
    self.dateLabel.text = [resultFormatter stringFromDate:date];
    
    //设置微博博文
    self.weiboTextLabel.text = dic[@"text"];
    
}


//通过block回调来返回按钮的tag
- (IBAction)tapCellButton:(id)sender {
    UIButton *button = sender;
    self.block(self, button.tag);
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end


2、上面的代码有点多,如果我们再加第二个cell(原微博带图片的)就简单多了,可以继承与上面的cell

iOS开发之新浪微博山寨版代码优化

  

  ImageTableViewCell.m的代码如下:(只把要添加的东西加上即可,是不是代码少多了)

@interface ImageTableViewCell()
@property (strong, nonatomic) IBOutlet UIImageView *contentImage;

@end

@implementation ImageTableViewCell
-(void)setDic:(NSDictionary *)dic
{
    [super setDic:dic];
    [self.contentImage setImageWithURL:[NSURL URLWithString:dic[@"thumbnail_pic"]]];
}
@end


3.第三种cell,是转发微博不带图片的,如下:

iOS开发之新浪微博山寨版代码优化

   ReTextTableViewCell也是继承于TextTableViewCell.  ReTextTableViewCell.m的代码如下:

@interface ReTextTableViewCell ()
@property (strong, nonatomic) IBOutlet UILabel *weiboTextLabel;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *textHeightConstraint;

@property (strong, nonatomic) IBOutlet UITextView *reTextView;

@end

@implementation ReTextTableViewCell

-(void)setDic:(NSDictionary *)dic
{
    [super setDic:dic];
    //移除约束
    [self removeConstraint:self.textHeightConstraint];
    
    //给据text的值求出textLabel的高度
    NSString *text = dic[@"text"];
    NSDictionary * dic1 = @{NSFontAttributeName: [UIFont systemFontOfSize:14]};
    
    CGRect frame = [text boundingRectWithSize:CGSizeMake(260, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic1 context:nil];
    
    //创建新的约束
    NSString *heightValue = [NSString stringWithFormat:@"V:[_weiboTextLabel(%lf)]",frame.size.height+10];
    NSArray *constraint = [NSLayoutConstraint constraintsWithVisualFormat:heightValue options:0 metrics:nil views:NSDictionaryOfVariableBindings(_weiboTextLabel)];
    
    self.textHeightConstraint = constraint[0];
    [self addConstraint:self.textHeightConstraint];
    
    self.weiboTextLabel.text = text;
    
    self.reTextView.text = dic[@"retweeted_status"][@"text"];

}
@end

  4.第四种cell就是转发带图片的啦,效果如下:

iOS开发之新浪微博山寨版代码优化

  因为第四种cell只比第三种cell多啦张图片,所以继承于第三种cell即可,代码如下:

#import "ReImageTableViewCell.h"

@interface ReImageTableViewCell()

@property (strong, nonatomic) IBOutlet UIImageView *contentImageView;

@end


@implementation ReImageTableViewCell

-(void)setDic:(NSDictionary *)dic
{
    [super setDic:dic];
    [self.contentImageView setImageWithURL:[NSURL URLWithString:dic[@"retweeted_status"][@"thumbnail_pic"]]];
}

@end


来看一下最终的运行效果:

iOS开发之新浪微博山寨版代码优化

  由上面的界面可以清楚的看到转发和评论的界面是基本一致的,所以我们在代码中可以用一个ViewController来控制这个视图,通过点击不同的按钮来拼接不同的url. 选择的业务逻辑如下:

if ([self.tag isEqualToValue:@2])
    {
        [self post:comments_create Content:@"comment"];
    }
    if ([self.tag isEqualToValue:@1])
    {
        [self post:repost_test Content:@"status"];
    }

 

  在转发页面中用到啦一个TextView, 我们给键盘上添加了一个Toolbar来进行键盘的回收,代码如下:

//TextView的键盘定制回收按钮
    UIToolbar * toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
    
    UIBarButtonItem * item1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(tapDone:)];
    UIBarButtonItem * item2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem * item3 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    toolBar.items = @[item2,item1,item3];
    
    self.commentsTextView.inputAccessoryView =toolBar;

  在要回调的方法中回收键盘:
1 - (IBAction)tapDone:(id)sender {
2     [self.commentsTextView resignFirstResponder];
3 }