生活随笔
收集整理的這篇文章主要介紹了
IOS开发基础之模拟科技头条项目案例32
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
IOS開發基礎之模擬科技頭條項目案例32
說說這個項目的技術要點核心 ,首先是異步網絡請求,block的回調,sdWebImage的使用。各個控件的使用,NSDate日期的轉換。自動適配屏幕工作,模型轉數組的使用,UITableViewCell的使用,等知識的使用。
主要源碼,以及項目源碼在我的主頁下面。
#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
;[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";
LJNewsCell
*cell
= [tableView dequeueReusableCellWithIdentifier
:reuseId
];
cell
.news
= self.newsList
[indexPath
.row
];return cell
;
}
@end
#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
#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";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){NSArray
*array
= [NSJSONSerialization JSONObjectWithData
:data options
:0 error
:NULL];NSMutableArray
*mArray
= [NSMutableArray arrayWithCapacity
:10];[array enumerateObjectsUsingBlock
:^(id _Nonnull obj
, NSUInteger idx
, BOOL
* _Nonnull stop
) {LJNews
*news
= [LJNews newsWithDict
:obj
];[mArray addObject
:news
];if(success
){success(mArray
.copy
);}}];}else{
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
#import <UIKit/UIKit.h>NS_ASSUME_NONNULL_BEGIN
@class LJNews
;
@interface LJNewsCell
: UITableViewCell
@property(nonatomic
,strong
) LJNews
*news
;
@endNS_ASSUME_NONNULL_END
#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
];
}
- (void)setNews
:(LJNews
*)news
{_news
= news
;self.titleView
.text
= news
.title
;self.summaryView
.text
= news
.summary
;self.sitenameView
.text
= news
.sitename
;self.addtimeView
.text
= news
.time
;[self.imageView sd_setImageWithURL
:[NSURL URLWithString
:news
.img
]];
}
@end
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
總結
以上是生活随笔為你收集整理的IOS开发基础之模拟科技头条项目案例32的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。