iOS开发之新浪微博山寨版代码优化
2023-09-14 08:57:28 时间
之前发表过一篇博客“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; @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"];
2、上面的代码有点多,如果我们再加第二个cell(原微博带图片的)就简单多了,可以继承与上面的cell
ImageTableViewCell.m的代码如下:(只把要添加的东西加上即可,是不是代码少多了)
@interface ImageTableViewCell() @property (strong, nonatomic) IBOutlet UIImageView *contentImage; @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; @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"]; @end4.第四种cell就是转发带图片的啦,效果如下:
因为第四种cell只比第三种cell多啦张图片,所以继承于第三种cell即可,代码如下:
#import "ReImageTableViewCell.h" @interface ReImageTableViewCell() @property (strong, nonatomic) IBOutlet UIImageView *contentImageView;
[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 }
[ios开发]-APP-上架流程 由于苹果的机制,在非越狱机器上安装必须通过官方的Appstore, 开发者开发好应用后上传Appstore,也需要通过审核等环节。 AppCan作为一个跨主流平台的一个开发平台,也对ipa包上传Appstore作了支持。 本文从三个流程来介绍如何实现AppCan在 线编译出ipa包,以及上传到苹果Appstore。
相关文章
- (NO.00005)iOS实现炸弹人游戏(一):游戏的整体规划设计
- 图文讲解:iOS App提交流程
- iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
- iOS开发之集成iOS9中的Core Spotlight Framework搜索App的内容
- IOS开发之显示微博表情
- IOS开发之微信山寨版
- iOS开发之SQLite--C语言接口规范(四) —— Result Values From A Query
- iOS开发之遍历Model类的属性并完善使用Runtime给Model类赋值
- iOS开发之多线程技术(NSThread、OperationQueue、GCD)
- iOS开发之网络编程--使用NSURLConnection实现大文件下载
- IOS开发--横向流水布局实现
- 给iOS开发新手送点福利,简述UIScrollView的属性和用法
- IOS 30多个iOS常用动画,带详细注释
- IOS开发之__bridge,__bridge_transfer和__bridge_retained
- iOS开发 - 文件压缩与解压缩
- iOS开发UI篇—简单介绍静态单元格的使用
- iOS开发UI篇—UITableview控件基本使用
- iOS开发UI篇—ios应用数据存储方式(偏好设置)
- 【IOS-COCOS2D游戏开发之七】在COCOS2D中添加/删除系统组件,并解决VIEW设置透明会影响VIEW中的其他组件的问题!【11月28日更新解决添加组件COCOS2D动画停止播放的BUG】
- iOS开发那些事--简单表视图
- iOS开发那些事--编写OCUnit测试方法-逻辑测试方法
- iOS swift 输入框(UITextField)密码明暗文切换
- iOS 代理(Delegate)和通知(NSNotification)
- [CoreBluetooth] API MISUSE: can only accept this command while in the powered on state(iOS蓝牙)
- IOS Object和javaScript相互调用
- iOS开发--漫谈内存管理(一)
- iOS swift 选择排序 冒泡排序 快速排序