zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

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

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"];

@end

4.第四种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。