日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/18 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IOS开发基础之模拟科技头条项目案例32 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。