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

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

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

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

cell对应的代码如下:

TextTableViewCell.h

1#import<UIKit/UIKit.h>23//TableView要回调的block,用于把cell中的按钮的tag传给TableView4typedefvoid(^MyCellBlock) (UITableViewCell * cell,inttag);56@interfaceTextTableViewCell : UITableViewCell7//接收block块8-(void)setMyCellBlock:(MyCellBlock) block;910//接收字典11-(void) setDic:(NSDictionary *)dic;1213@end

TextTableViewCell.m(带图片的cell继承于这个cell)

1#import"TextTableViewCell.h"23@interfaceTextTableViewCell()45@PRoperty (strong, nonatomic) IBOutlet UIImageView *headImage;6@property (strong, nonatomic) IBOutlet UILabel *nameLabel;7@property (strong, nonatomic) IBOutlet UILabel *dateLabel;8@property (strong, nonatomic) IBOutlet UILabel *weiboTextLabel;910@property (strong, nonatomic) NSDictionary *dic;11@property (strong, nonatomic) MyCellBlock block;1213@end1415@implementationTextTableViewCell1617//获取传入的block块18-(void)setMyCellBlock:(MyCellBlock)block19{20self.block =block;21}2223//获取传入的参数,用于给我们的cell中的标签赋值24-(void) setDic:(NSDictionary *)dic25{2627//设置头像28[self.headImage setImageWithURL:[NSURL URLWithString:dic[@"user"][@"profile_image_url"]]];2930//设置昵称31self.nameLabel.text = dic[@"user"][@"name"];3233//设置时间34NSDateFormatter *iosDateFormater=[[NSDateFormatter alloc]init];35iosDateFormater.dateFormat=@"EEE MMM d HH:mm:ss Z yyyy";3637//必须设置,否则无法解析38iosDateFormater.locale=[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];39NSDate *date=[iosDateFormater dateFromString:dic[@"created_at"]];4041//目的格式42NSDateFormatter *resultFormatter=[[NSDateFormatter alloc]init];43[resultFormatter setDateFormat:@"MM月dd日 HH:mm"];44self.dateLabel.text =[resultFormatter stringFromDate:date];4546//设置微博博文47self.weiboTextLabel.text = dic[@"text"];4849}505152//通过block回调来返回按钮的tag53- (IBAction)tapCellButton:(id)sender {54UIButton *button =sender;55self.block(self, button.tag);56}5758- (void)awakeFromNib59{60//Initialization code61}6263- (void)setSelected:(BOOL)selected animated:(BOOL)animated64{65[super setSelected:selected animated:animated];6667//Configure the view for the selected state68}6970@end

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

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

@interfaceImageTableViewCell()

@property (strong, nonatomic) IBOutlet UIImageView*contentImage;@end@implementationImageTableViewCell-(void)setDic:(NSDictionary *)dic

{

[super setDic:dic];

[self.contentImage setImageWithURL:[NSURL URLWithString:dic[@"thumbnail_pic"]]];

}@end

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

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

1@interfaceReTextTableViewCell ()2@property (strong, nonatomic) IBOutlet UILabel *weiboTextLabel;3@property (strong, nonatomic) IBOutlet NSLayoutConstraint *textHeightConstraint;45@property (strong, nonatomic) IBOutlet UITextView *reTextView;67@end89@implementationReTextTableViewCell1011-(void)setDic:(NSDictionary *)dic12{13[super setDic:dic];14//移除约束15[self removeConstraint:self.textHeightConstraint];1617//给据text的值求出textLabel的高度18NSString *text = dic[@"text"];19NSDictionary * dic1 = @{NSFontAttributeName: [UIFont systemFontOfSize:14]};2021CGRect frame = [text boundingRectWithSize:CGSizeMake(260,1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic1 context:nil];2223//创建新的约束24NSString *heightValue = [NSString stringWithFormat:@"V:[_weiboTextLabel(%lf)]",frame.size.height+10];25NSArray *constraint = [NSLayoutConstraint constraintsWithVisualFormat:heightValue options:0metrics:nil views:NSDictionaryOfVariableBindings(_weiboTextLabel)];2627self.textHeightConstraint = constraint[0];28[self addConstraint:self.textHeightConstraint];2930self.weiboTextLabel.text =text;3132self.reTextView.text = dic[@"retweeted_status"][@"text"];3334}35@end

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

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

#import"ReImageTableViewCell.h"@interfaceReImageTableViewCell()

@property (strong, nonatomic) IBOutlet UIImageView*contentImageView;@end@implementationReImageTableViewCell-(void)setDic:(NSDictionary *)dic

{

[super setDic:dic];

[self.contentImageView setImageWithURL:[NSURL URLWithString:dic[@"retweeted_status"][@"thumbnail_pic"]]];

}@end

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

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

1if([self.tag isEqualToValue:@2])2{3[self post:comments_create Content:@"comment"];4}5if([self.tag isEqualToValue:@1])6{7[self post:repost_test Content:@"status"];8}

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

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

在要回调的方法中回收键盘:

1- (IBAction)tapDone:(id)sender {2[self.commentsTextView resignFirstResponder];3}

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航