博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS开发之新浪微博山寨版代码优化
阅读量:7075 次
发布时间:2019-06-28

本文共 5434 字,大约阅读时间需要 18 分钟。

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

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

  cell对应的代码如下:

  TextTableViewCell.h

#import 
//TableView要回调的block,用于把cell中的按钮的tag传给TableViewtypedef 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];    iosDateFormater.dateFormat=@"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

  

  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,是转发微博不带图片的,如下:

   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就是转发带图片的啦,效果如下:

  因为第四种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

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

  由上面的界面可以清楚的看到转发和评论的界面是基本一致的,所以我们在代码中可以用一个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 }

转载地址:http://rouml.baihongyu.com/

你可能感兴趣的文章
如何在Kettle4.2上面实现cassandra的输入与输出
查看>>
Boost中的一些实用技术
查看>>
数据库优化和SQL操作的相关题目
查看>>
Oracle创建表空间
查看>>
java使用Executor(执行器)管理线程
查看>>
iOS开发Swift篇—(十)方法
查看>>
[翻译] UPCardsCarousel
查看>>
win8 开发之旅(19) --足球游戏揭秘6
查看>>
翻页效果
查看>>
UIGestureRecognizerState
查看>>
Lua非常有用的工具——递归打印表数据
查看>>
(九十四)函数和二维数组
查看>>
Android ListView监听上下滑动(判断是否显示返回顶部按钮)
查看>>
跟着实例学习ZooKeeper的用法: Barrier
查看>>
JAVA生成二维码(zxing)
查看>>
BUG系列
查看>>
成为优秀Java程序员的10大技巧
查看>>
一个16年毕业生所经历的php面试
查看>>
AAC架构系列一(初识)
查看>>
react-native-echarts 在手机上 图表出现滚动条解决方法
查看>>