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

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

  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];
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:]}; CGRect frame = [text boundingRectWithSize:CGSizeMake(, ) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic1 context:nil]; //创建新的约束
NSString *heightValue = [NSString stringWithFormat:@"V:[_weiboTextLabel(%lf)]",frame.size.height+];
NSArray *constraint = [NSLayoutConstraint constraintsWithVisualFormat:heightValue options: metrics:nil views:NSDictionaryOfVariableBindings(_weiboTextLabel)]; self.textHeightConstraint = constraint[];
[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:@])
{
[self post:comments_create Content:@"comment"];
}
if ([self.tag isEqualToValue:@])
{
[self post:repost_test Content:@"status"];
}

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

     //TextView的键盘定制回收按钮
UIToolbar * toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(, , , )]; 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;

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

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

iOS开发之新浪微博山寨版代码优化的更多相关文章

  1. IOS开发之新浪微博OAuth2

    说明:微博开放接口的调用,如发微博.关注等,都是需要获取用户身份认证的.目前微博开放平台用户身份鉴权主要采用的是OAuth2.0.为了方便开发者开发.测试自己的应用. OAuth2.0较1.0相比,整 ...

  2. IOS开发之微信山寨版

    为了犒劳自己的学习内容,就山寨个微信的视图控制吧.拿着微信,仔细的看了一下,主要用到了TabBarController以及配置TabBarItem, NavigationController以及配置N ...

  3. 我的iOS开发系列博文

    之前目录性的总结了发表过的关于OC方面的文章,今天在目录性的总结一下有关iOS开发的文章.走过路过不要错过哦,今天的博文也全都是干货.写技术博客与大家交流一下思想也是不错的. 下面是我的技术博客中有关 ...

  4. iOS开发之山寨版新浪微博小结

    在之前的博客IOS开发之新浪围脖中获取微博的内容是使用我自己的access_token来请求的数据,那么如何让其他用户也能登陆并获取自己的微博内容呢?接下来就是OAuth和SSO出场的时候啦.OAut ...

  5. iOS开发之微信聊天工具栏的封装

    之前山寨了一个新浪微博(iOS开发之山寨版新浪微博小结),这几天就山寨个微信吧.之前已经把微信的视图结构简单的拖了一下(IOS开发之微信山寨版),今天就开始给微信加上具体的实现功能,那么就先从微信的聊 ...

  6. iOS开发系列--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook系统服务开发汇总

    --系统应用与系统服务 iOS开发过程中有时候难免会使用iOS内置的一些应用软件和服务,例如QQ通讯录.微信电话本会使用iOS的通讯录,一些第三方软件会在应用内发送短信等.今天将和大家一起学习如何使用 ...

  7. iOS开发系列--网络开发

    概览 大部分应用程序都或多或少会牵扯到网络开发,例如说新浪微博.微信等,这些应用本身可能采用iOS开发,但是所有的数据支撑都是基于后台网络服务器的.如今,网络编程越来越普遍,孤立的应用通常是没有生命力 ...

  8. iOS开发之浅谈MVVM的架构设计与团队协作

    今天写这篇博客是想达到抛砖引玉的作用,想与大家交流一下思想,相互学习,博文中有不足之处还望大家批评指正.本篇博客的内容沿袭以往博客的风格,也是以干货为主,偶尔扯扯咸蛋(哈哈~不好好工作又开始发表博客啦 ...

  9. IOS开发基础知识碎片-导航

    1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...

随机推荐

  1. HTTP协议入门要点

    应用层协议.基于tcp HTTP/0.9 命令 GET 特点 服务器只能回应HTML字符串 服务器发送完毕后就关闭tcp连接 HTTP/1.0 命令 GET POST HEAD 特点 每次通信都必须包 ...

  2. Odoo 二次开发教程(三)-第一个Model及Form、Tree视图

    创建完我们的模块,接下来我们就要为我们的模块添加一些对象.今天我们将要创建一个学生对象(tech.student)和一些基本的属性,并将用form和tree视图将其展示出来: 一. 创建tech.st ...

  3. tab22

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. finally回收资源

    Java中的垃圾回收机制,也就是GC不会回收任何物理资源,垃圾回收机制只回收堆内存中对象所占用的内存,所以其他的物理资源需要用finally来回收. 如果try块中的某条语句引起了异常,该异常就会被c ...

  5. Zybo GPIO Demo Run Embedded Linux

    1.Environment Ubuntu 12.04 x86_64 Vivado 2013.4 SDK 2013.4   2.Pre-requisites 2.1 CodeSourcery arm-g ...

  6. 如何使用Goolge Timeline工具

    网上中文的资料版本比较老,找到一个新版本的英文介绍,翻一下,原文:https://developers.google.com/web/tools/chrome-devtools/profile/eva ...

  7. 【实战Java高并发程序设计 3】带有时间戳的对象引用:AtomicStampedReference

    [实战Java高并发程序设计 1]Java中的指针:Unsafe类 [实战Java高并发程序设计 2]无锁的对象引用:AtomicReference AtomicReference无法解决上述问题的根 ...

  8. QSort函数对不同类型数据快速排序浅谈

    一.对int类型数组排序 int num[100]; int cmp ( const void *a , const void *b ){return *(int *)a - *(int *)b;} ...

  9. org.apache.jasper.JasperException:省略"/html/sysmaintain/authority/user/../../module/verify_login.jsp" not found

    说明了JSP页面里引用安全登录页面的jsp路径代码:<%@ include file="../../module/verify_login.jsp"%>这句代码引用的路 ...

  10. SQL Server 变更数据捕获(CDC)监控表数据

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 实现过程(Realization) 补充说明(Addon) 参考文献(References) ...