首页 技术 正文
技术 2022年11月21日
0 收藏 436 点赞 4,614 浏览 4907 个字

  之前发表过一篇博客“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];
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

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

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:@])
{
[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];
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,965
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,486
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,331
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,114
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,747
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,781