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

简介:

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

@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:@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 }

相关文章
|
2月前
|
API 数据安全/隐私保护 iOS开发
利用uni-app 开发的iOS app 发布到App Store全流程
利用uni-app 开发的iOS app 发布到App Store全流程
106 3
|
4月前
|
存储 iOS开发
iOS 开发,如何进行应用的本地化(Localization)?
iOS 开发,如何进行应用的本地化(Localization)?
123 2
|
4月前
|
存储 数据建模 数据库
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
43 0
|
4月前
|
安全 编译器 Swift
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
106 2
|
2月前
|
API 开发工具 Android开发
iOS 和 Android 平台的开发有哪些主要区别?
iOS与Android开发区别:iOS用Objective-C/Swift,App Store唯一下载渠道;Android用Java/Kotlin,多商店发布(如Google Play、华为市场)。设计上,iOS简洁一致,Android灵活可定制。开发工具,iOS用Xcode,Android用Android Studio。硬件和系统多样性,iOS统一,Android复杂。权限管理、审核流程及API各有特点,开发者需依据目标平台特性进行选择。
38 3
|
12天前
|
前端开发 Android开发 iOS开发
【Flutter前端技术开发专栏】Flutter在Android与iOS上的性能对比
【4月更文挑战第30天】Flutter 框架实现跨平台移动应用,通过一致的 UI 渲染(Skia 引擎)、热重载功能和响应式框架提高开发效率和用户体验。然而,Android 和 iOS 的系统差异、渲染机制及编译过程影响性能。性能对比显示,iOS 可能因硬件优化提供更流畅体验,而 Android 更具灵活性和广泛硬件支持。开发者可采用代码、资源优化和特定平台优化策略,利用性能分析工具提升应用性能。
【Flutter前端技术开发专栏】Flutter在Android与iOS上的性能对比
|
12天前
|
存储 Swift iOS开发
使用Swift开发一个简单的iOS应用的详细步骤。
使用Swift开发iOS应用的步骤包括:创建Xcode项目,设计界面(Storyboard或代码),定义数据模型,实现业务逻辑,连接界面和逻辑,处理数据存储(如Core Data),添加网络请求(必要时),调试与测试,根据测试结果优化改进,最后提交至App Store或其它平台发布。
32 0
|
12天前
|
安全 Swift iOS开发
【Swift 开发专栏】Swift 与 UIKit:构建 iOS 应用界面
【4月更文挑战第30天】本文探讨了Swift和UIKit在构建iOS应用界面的关键技术和实践方法。Swift的简洁语法、类型安全和高效编程模型,加上与UIKit的紧密集成,使开发者能便捷地创建用户界面。UIKit提供视图、控制器、布局、动画和事件处理等功能,支持灵活的界面设计。实践中,遵循设计原则,合理组织视图层次,运用布局和动画,以及实现响应式设计,能提升界面质量和用户体验。文章通过登录、列表和详情界面的实际案例展示了Swift与UIKit的结合应用。
|
12天前
|
存储 安全 Swift
【Swift 开发专栏】使用 Swift 开发一个简单的 iOS 应用
【4月更文挑战第30天】本文介绍了使用 Swift 开发简单 iOS 待办事项应用的步骤。首先,阐述了 iOS 开发的吸引力及 Swift 语言的优势。接着,详细说明了应用的需求和设计,包括添加、查看和删除待办事项的功能。开发步骤包括创建项目、界面搭建、数据存储、功能实现,并提供了相关代码示例。最后,强调了实际开发中需注意的细节和优化,旨在帮助初学者掌握 Swift 和 iOS 开发基础。
|
20天前
|
iOS开发 开发者 UED
利用SwiftUI构建动态列表:iOS开发的新范式
【4月更文挑战第22天】在本文中,我们将深入探讨如何使用SwiftUI来创建动态列表。SwiftUI是苹果最新推出的用户界面工具集,它允许开发者以声明式的方式描述用户界面,从而简化了代码的复杂性。我们将通过具体的代码实例,展示如何利用SwiftUI的List和ForEach视图来创建动态列表,并讨论其在实际开发中的应用。
20 2