IOS开发基础之模拟科技头条项目案例32

说说这个项目的技术要点核心 ,首先是异步网络请求,block的回调,sdWebImage的使用。各个控件的使用,NSDate日期的转换。自动适配屏幕工作,模型转数组的使用,UITableViewCell的使用,等知识的使用。
主要源码,以及项目源码在我的主页下面。

//
//  ViewController.m
//  32-科技头条
//
//  Created by 鲁军 on 2021/3/9.
//#import "ViewController.h"
#import "LJNews.h"
#import "LJNewsCell.h"
@interface ViewController () <UITableViewDataSource>
@property(nonatomic,strong)NSArray *newsList;
@end
@implementation ViewController
-(void)setNewsList:(NSArray *)newsList{_newsList = newsList;//等着加载完数据  重新刷新tableView[self.tableView reloadData];//结束下拉刷新[self.refreshControl endRefreshing];
}
- (void)viewDidLoad {[super viewDidLoad];self.refreshControl.tintColor = [UIColor blueColor];self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"正在拼命加载..." attributes:@{NSForegroundColorAttributeName:[UIColor redColor]}];[self loadNews];
}
//发送异步请求获取数据
-(IBAction)loadNews{[LJNews newsWithSuccess:^(NSArray * _Nonnull array) {self.newsList = array;} error:^{NSLog(@"error");}];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{return self.newsList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{static NSString *reuseId = @"news";
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];LJNewsCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];//    if(cell==nil){//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId];
//    }// LJNews *news = self.newsList[indexPath.row];//cell.textLabel.text = news.title;cell.news = self.newsList[indexPath.row];return  cell;
}
@end
//
//  LJNews.h
//  32-科技头条
//
//  Created by 鲁军 on 2021/3/9.
//#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface LJNews : NSObject
@property(nonatomic,copy)NSString *title;
@property(nonatomic,copy)NSString *summary;
@property(nonatomic,copy)NSString *img;
@property(nonatomic,copy)NSString *sitename;
@property(nonatomic,strong)NSNumber *addtime;
@property(nonatomic,copy,readonly)NSString *time;
+(instancetype)newsWithDict:(NSDictionary *)dict;
+(void)newsWithSuccess:(void(^)(NSArray *array))success error:(void(^)()) error;
@endNS_ASSUME_NONNULL_END
//
//  LJNews.m
//  32-科技头条
//
//  Created by 鲁军 on 2021/3/9.
#import "LJNews.h"
@implementation LJNews
+(instancetype)newsWithDict:(NSDictionary *)dict{LJNews *news = [self new];[news setValuesForKeysWithDictionary:dict];return news;
}
- (NSString *)time{//把数字的日期。 转成 日期对象NSDate *date = [NSDate dateWithTimeIntervalSince1970:self.addtime.intValue];NSCalendar *calendar = [NSCalendar currentCalendar];//两个日期相减。获取指定的日期部分NSDateComponents *components = [calendar components:NSCalendarUnitMinute fromDate:date toDate:[NSDate date] options:0];if(components.minute < 60){return [NSString stringWithFormat:@"%zd分钟以前",components.minute];}//获取小时if(components.hour < 24){return [NSString stringWithFormat:@"%zd小时以前",components.hour];}//获取日期NSDateFormatter *ndf = [[NSDateFormatter alloc] init];
//    ndf.dateFormat = @"yyyy-MM-dd HH:mm:ss";ndf.dateFormat = @"yyyy-MM-dd HH:mm";return [ndf stringFromDate:date];
}
+(void)newsWithSuccess:(void(^)(NSArray *array))success error:(void(^)()) error{NSURL *url = [NSURL URLWithString:@"http://localhost:8080/news"];NSURLRequest *request = [NSURLRequest requestWithURL:url];[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {if(connectionError){NSLog(@"连接错误 %@",connectionError);return;}NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;if(httpResponse.statusCode==200||httpResponse.statusCode==304){//解析数据//解析数据  json形式的字符串转成OC对象NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];//NSLog(@"%@",array);//字典转模型NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:10];[array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {//  NSLog(@"%@",obj);LJNews *news = [LJNews newsWithDict:obj];[mArray addObject:news];//调用成公的回调if(success){success(mArray.copy);}}];// NSLog(@"%@",mArray);}else{//            NSLog(@"服务器内部错误");if(error){error();}}}];
}
//当模型多余 属性的时候 需要重写该方法   忽略
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{}
-(NSString *)description {return [NSString stringWithFormat:@"%@{title:%@,summary:%@,img:%@.sitename:%@,addtime:%d}",[super description],self.title,self.summary,self.img,self.sitename,self.addtime.intValue];
}
@end
//
//  LJNewsCell.h
//  32-科技头条
//
//  Created by 鲁军 on 2021/3/14.
//#import <UIKit/UIKit.h>NS_ASSUME_NONNULL_BEGIN
@class LJNews;
@interface LJNewsCell : UITableViewCell
@property(nonatomic,strong) LJNews *news;
@endNS_ASSUME_NONNULL_END
//
//  LJNewsCell.m
//  32-科技头条
//
//  Created by 鲁军 on 2021/3/14.
//#import "LJNewsCell.h"
#import "LJNews.h"
#import "UIImageView+WebCache.h"
@interface LJNewsCell ()
@property (weak, nonatomic) IBOutlet UIImageView *imgView;
@property (weak, nonatomic) IBOutlet UILabel *titleView;
@property (weak, nonatomic) IBOutlet UILabel *summaryView;
@property (weak, nonatomic) IBOutlet UILabel *sitenameView;
@property (weak, nonatomic) IBOutlet UILabel *addtimeView;
@end
@implementation LJNewsCell
- (void)layoutSubviews{[super layoutSubviews];//    判断news.title 的长度。和label的长度的比较/*  CGFloat titleLength = [self.news.title sizeWithAttributes:@{NSFontAttributeName:self.titleView.font}].width;if(titleLength>self.titleView.frame.size.width){self.summaryView.hidden = YES;}else{self.summaryView.hidden = NO;}*/
}
- (void)setNews:(LJNews *)news{_news = news;self.titleView.text = news.title;self.summaryView.text = news.summary;self.sitenameView.text = news.sitename;//self.addtimeView.text = [NSString stringWithFormat:@"%d",news.addtime.intValue];self.addtimeView.text = news.time;// [self.imgView sd_setImageWithURL:[NSURL URLWithString:news.img]];// [self.imageView sizeToFit];[self.imageView sd_setImageWithURL:[NSURL URLWithString:news.img]];
}
@end

IOS开发基础之模拟科技头条项目案例32相关推荐

  1. IOS开发基础之微博项目第1天-OC版

    IOS开发基础之微博项目第1天-OC版 纯代码创建的项目,具有参考价值 该资料来自2014年7月3号,虽然时间过去较长,但是oc和swift不同,oc语法迭代更新慢 具有一定的参考意义 涉及xib加载 ...

  2. IOS开发基础之手势解锁项目案例

    IOS开发基础之手势解锁项目案例 项目最终实现效果. 由于缺少红色的error背景图.我自己从安卓项目找到一个手势解锁,然后通过ps添加粉红色的红圈,才得以解决.为了分享给大家源码,github和本地 ...

  3. IOS开发基础之微博项目

    IOS开发基础之微博项目 关键性代码 // // NJViewController.m // 06-预习-微博(通过代码自定义cell)// #import "NJViewControlle ...

  4. IOS开发基础之汽车品牌项目-14

    IOS开发基础之汽车品牌项目-14 // // ViewController.m // 16-汽车品牌展示02 // // Created by 鲁军 on 2021/2/3. //#import & ...

  5. IOS开发基础之UI的喜马拉雅的项目-10

    IOS开发基础之UI的喜马拉雅的项目-10 // // ViewController.m // 10-喜马拉雅 // // Created by 鲁军 on 2021/2/2. //#import & ...

  6. iOS开发基础知识--碎片44

    iOS开发基础知识--碎片44  iOS开发基础知识--碎片44 1:App跳转至系统Settings 跳转在IOS8以上跟以下是有区别的,如果是IOS8以上可以如下设置: NSURL *url = ...

  7. IOS开发基础之OC的Block入门_Day09-Block

    IOS开发基础之OC的Block入门_Day09-Block block是oc的重要的基础知识,重点之重.跟协议一样重要,是进行函数回调重要手段.在后续的UI学习具有举足轻重的地位.学会基础的bloc ...

  8. IOS开发基础之音频工具类封装AVAudioPlayer

    IOS开发基础之音频工具类封装AVAudioPlayer 源码在我的主页下面 ,项目名称是AVAudioPlayer 关键性代码 工具类的封装 // // LJAudioTool.h // AVAud ...

  9. IOS开发基础之socket的使用

    IOS开发基础之socket的使用 socket是纯C语言的,跨平台的 第一章: socket 的演示 // ViewController.m // 29-socket演示 // Created by ...

最新文章

  1. volley框架使用
  2. sqlserⅴer随机函数_sql server 函数--rand() 生成整数的随机数
  3. boost::mpl模块实现joint_view相关的测试程序
  4. C# winform WebBrowser怎么获取js中的变量的值?怎么触发js的事件?
  5. mysql导出txt到client_mysql导出导入txt以及sftp自动下载(一)
  6. 华为抢购助手_华为MateBook 13轻薄本:出差者的首选,学生党的福音
  7. 【渝粤教育】国家开放大学2018年秋季 0161-22T教师职业道德 参考试题
  8. 量子计算机如何确定量子状态,量子计算机六个量子位足以确定三个简单分子的基态...
  9. 频段表_5G频段范围之:频段3.3GHz-4.2GHz (n77,n78)
  10. HTML5 学习准备1
  11. navicat8 for mysql注册码
  12. 公司要一个网站,是选模板建站还是定制化建站?
  13. android4.2.2飞歌导航,智能车机新典范 飞歌G6S荣耀版导航鉴赏
  14. Ubuntu18.04安装有道词典
  15. 一个MOS 电平转换电路引发的思考可避免更多的坑
  16. 保险精算--第8周作业
  17. 多用户MIMO在移动通信系统中的应用
  18. RabbitMQ之交换机
  19. No constructor found in com.think.pojo.Curd matching [java.lang.Long, java
  20. 五个经典漏斗模型,看漏斗思维穿透流程化的本质

热门文章

  1. mysql包导入之后idea仍然报错_IDEA连接MySQL报错怎么弄?
  2. 查看家庭组组计算机用户名密码是什么,windows10系统如何查看家庭组密码
  3. 两部手机怎样才能把数据都传过来_我把魅族换成荣耀,30G的数据文件该如何一键转移?...
  4. 考研失败了,该何去何从?
  5. 嵌入式的坑在哪方面?
  6. 两篇同年硕士论文高度雷同!电子科技大学回应:启动调查!
  7. 基于matlab的能级_波函数及几率密度图形的绘制,基于MATLAB的能级波函数及几率密度图形的绘制.pdf...
  8. 二元函数泰勒公式例题_高等数学入门——二元函数可微性的判断方法总结
  9. php static method,php 类方法用static::hello(); 等同于 $this-hello();吗?
  10. css-net 中华版,使用C#代码选择CSS样式(ASP.net)