自動處理鍵盤事件的第三方庫 IQKeyboardManager
MJRefresh 下拉刷新孵睬,上拉加載的控件
提示:在block中使用self中的屬性醒颖、成員變量方法需要使用弱引用
__weak __weak typeof(self) weakSelf = self;
//導(dǎo)入
#import <MJRefresh.h>
/** 創(chuàng)建header Blockf方法 */
+ (instancetype)headerWithRefreshingBlock:(MJRefreshComponentRefreshingBlock)refreshingBlock;
/** 創(chuàng)建header 調(diào)用方法SEL*/
+ (instancetype)headerWithRefreshingTarget:(id)target refreshingAction:(SEL)action;
//這個方法刷新時沒有footer的文字膀藐,直接刷新
tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock
注意:
1.每次上拉加載更多數(shù)據(jù)后,要結(jié)束刷新狀態(tài)减响,才能才次進(jìn)行刷新
[weakSelf.centerTableView.mj_footer endRefreshing];
2.在刷新的過程中需要防止用戶重復(fù)刷新,否則會導(dǎo)致數(shù)據(jù)加載丟失
// 在數(shù)據(jù)獲取失敗的地方減少頁數(shù)
if (weakSelf.currentPage > 1) {
weakSelf.currentPage--;
}
//上拉刷新數(shù)據(jù)
[self.appListTableView.mj_footer beginRefreshing];
//下拉加載數(shù)據(jù)
[self.appListTableView.mj_header beginRefreshing]
例:
block方法刷新數(shù)據(jù),對數(shù)據(jù)傳值
//上拉加載
self.appListTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//因?yàn)槭巧侠⑿拢酗@示數(shù)據(jù)的頁數(shù)都是第一頁竹椒,最新的數(shù)據(jù)
weakSelf.currentPage = 1;
//隱藏footer,在加載的過程中防止用戶下拉刷新米辐,導(dǎo)致數(shù)據(jù)錯誤
weakSelf.appListTableView.mj_footer.hidden = YES;
//請求數(shù)據(jù),自己設(shè)置的方法书释,導(dǎo)入第幾頁的第幾個數(shù)據(jù)翘贮,分類ID
[weakSelf requestAllListWithPage:weakSelf.currentPage searchText:weakSelf.searchKeyWord categoryId:weakSelf.categoryId];
}];
//下拉刷新
self.appListTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//在刷新的過程中需要防止用戶重復(fù)刷新,導(dǎo)致數(shù)據(jù)加載丟失
weakSelf.currentPage++;
// 隱藏header爆惧,在刷新的過程中防止用戶上拉加載狸页,導(dǎo)致數(shù)據(jù)錯誤
weakSelf.appListTableView.mj_header.hidden = YES;
//請求數(shù)據(jù),自己設(shè)置的方法扯再,導(dǎo)入第幾頁的第幾個數(shù)據(jù)芍耘,分類ID
[weakSelf requestAllListWithPage:weakSelf.currentPage searchText:weakSelf.searchKeyWord categoryId:weakSelf.categoryId];
}];
數(shù)據(jù)加載成功后需要:
//結(jié)束下拉加載數(shù)據(jù)
[weakSelf.appListTableView.mj_header endRefreshing];
//結(jié)束上拉刷新數(shù)據(jù)
[weakSelf.appListTableView.mj_footer endRefreshing];
//取消對上拉和下拉的隱藏,讓用戶可以繼續(xù)刷新數(shù)據(jù)
weakSelf.appListTableView.mj_header.hidden = NO;
weakSelf.appListTableView.mj_footer.hidden = NO;
//對請求的數(shù)據(jù)進(jìn)行判斷熄阻,提示用戶數(shù)據(jù)全部請求完畢
if (weakSelf.appListArray.count(請求數(shù)據(jù)的個數(shù))>= totalCount(總數(shù)據(jù)的個數(shù)) ){
// 提示用戶數(shù)據(jù)全部請求完畢
[weakSelf.appListTableView.mj_footer endRefreshingWithNoMoreData];
}else {
// 當(dāng) 當(dāng)前請求的數(shù)據(jù)小于totalCount時(總數(shù)據(jù)的個數(shù))斋竞,重置footer的狀態(tài)
//重置沒有更多的數(shù)據(jù)(消除沒有更多數(shù)據(jù)的狀態(tài))
[weakSelf.appListTableView.mj_footer resetNoMoreData];
}
對header的刷新進(jìn)行文字設(shè)置
//先對創(chuàng)建MJRefreshNormalHeader 對象,在獲取MJRefreshNormalHeader 的屬性秃殉,進(jìn)行文字設(shè)置
MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock
[header setTitle:@"爺爺?shù)鹊煤眯量喟映酰靵矸鑫? forState:MJRefreshStateIdle];
[header setTitle:@"爺爺摔倒了" forState:MJRefreshStatePulling];
[header setTitle:@"開開心心扶老爺爺" forState:MJRefreshStateRefreshing];
//最后千萬別忘記, 設(shè)置要刷新的對象
subjectTableView.mj_header = header
//開始加載數(shù)據(jù)
[_subjectTableView.mj_header beginRefreshing]
AFNetworking 請求網(wǎng)絡(luò)數(shù)據(jù)
// 結(jié)束之前的所有請求
[self.manager.tasks makeObjectsPerformSelector:@selector(cancel)];
//取消AFNetworking的所有任務(wù)钾军,使不可用
[self.manager invalidateSessionCancelingTasks:YES];
1.先要創(chuàng)建對象
// 網(wǎng)絡(luò)請求
@property (nonatomic, strong) AFHTTPSessionManager * httpManager;
//響應(yīng)JSON數(shù)據(jù)
- (AFHTTPSessionManager *)httpManager
{
if (!_httpManager) {
_httpManager = [AFHTTPSessionManager manager];
// 設(shè)置JSON數(shù)據(jù)序列化鳄袍,將JSON數(shù)據(jù)轉(zhuǎn)換為字典或者數(shù)組
_httpManager.responseSerializer = [AFJSONResponseSerializer serializer];
// 在序列化器中追加一個類型,text/html這個類型是不支持的吏恭,text/json, application/json
_httpManager.responseSerializer.acceptableContentTypes = [_httpManager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
}
return _httpManager;
}
//請求 JSON數(shù)據(jù) requestSerializer
if (!_afHttpMagaer) {
_afHttpMagaer = [AFHTTPSessionManager manager];
//請求JSON數(shù)據(jù)(requestSerializer)
_afHttpMagaer.requestSerializer = [AFJSONRequestSerializer
serializer];
// 在序列化器中追加一個類型拗小,text/html這個類型是不支持的,text/json, application/json
_afHttpMagaer.responseSerializer.acceptableContentTypes = [_afHttpMagaer.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
}
//請求GET數(shù)據(jù)
[self.httpManager GET:(NSString *請求數(shù)據(jù)) parameters:(id一般寫nil) success:^(NSURLSessionDataTask *task任務(wù), id responseObject返回的數(shù)據(jù)) {
請求成功會到這里
在block中使用self中的屬性樱哼、成員變量方法需要使用弱引用
__weak __weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
// 不要忘記刷新數(shù)據(jù) UITableView
[weakSelf.appListTableView reloadData];
[weakSelf.appListTableView reloadData];
//數(shù)據(jù)請求成功后哀九,要返回主線程對UI界面進(jìn)行賦值
});
} failure:^(NSURLSessionDataTask *task, NSError *error) {
請求失敗回到這里
}];
//POST請求
//如果請求是字典中包含字典
{
"record": {
"page": 1
}
}
//需要這樣寫:
NSDictionary *dict1=@{@"page":@1};
NSDictionary *dic2=@{@"record":dict1};
//或者這樣寫
NSDictionary *dd = @{@"record":@{@"page":@1}};
NSString *url = @"http://life.fotile.com/fotileApp/course/getRecipeList";
[self.afHttpMagaer POST:url parameters:dic2 progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
ZBLog(@"美食=%@", responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
ZBLog(@"%@", error.localizedDescription);
}];
YYModel 將數(shù)據(jù)轉(zhuǎn)化模型
1.第一步在模型類導(dǎo)入YYModel
@interface ZB_Model : NSObject <YYModel>
2.將數(shù)據(jù)轉(zhuǎn)化為數(shù)組/模型
yy_modelArrayWithClass 要轉(zhuǎn)化的數(shù)組類型
json 需要轉(zhuǎn)換成模型的json數(shù)據(jù)
NSArray *appModelArray = [NSArray yy_modelArrayWithClass:[ZB_Model class] json:appArray];
NSDictionary *appModelArray = [NSDictionary yy_modelDictionaryWithClass:<#(__unsafe_unretained Class)#> json:<#(id)#>]
// 關(guān)聯(lián)JSON數(shù)據(jù)中的key和類中的屬性
// 字典中的key為屬性,value為JSON數(shù)據(jù)中的key
// 當(dāng)屬性和JSON數(shù)據(jù)中的key不一致時會用到該方法搅幅,做映射
+ (NSDictionary *)modelCustomPropertyMapper
{
return @{@"desc":@"description"};
}
// 當(dāng)屬性中為數(shù)組時勾栗,需要關(guān)聯(lián)其他類,使得數(shù)組中存放該類的對象
// 字典中的key為當(dāng)前類的屬性盏筐,value為要關(guān)聯(lián)的類的class
+ (NSDictionary *)modelContainerPropertyGenericClass
{
return @{@"applications" : [SubjectAppModel class]};
}
Masonry 建立約束
http://adad184.com/2014/09/28/use-masonry-to-quick-solve-autolayout/
//先關(guān)閉以前的約束
self.automaticallyAdjustsScrollViewInsets = NO;
//appListTableViewd的X,Y,寬,高等于當(dāng)前的View
[self.appListTableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(weakSelf.view);
}];
[self.coverImage mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(weakSelf.backgroundV.mas_top).offset(5);
make.left.mas_equalTo(weakSelf.backgroundV.mas_left).offset(5);
make.right.mas_equalTo(weakSelf.backgroundV.mas_right).offset(-5);
//高為寬的倍數(shù)
make.height.mas_equalTo(weakSelf.coverImage.mas_width).multipliedBy(301/345.0f);
}];
更新約束
// 告訴self.view約束需要更新
[self.view setNeedsUpdateConstraints];
// 調(diào)用此方法告訴self.view檢測是否需要更新約束围俘,若需要則更新
[self.view updateConstraintsIfNeeded];
[self.view layoutIfNeeded];
SDWebImage使用方法
http://www.reibang.com/p/4191017c8b39/comments/1566836
SDWebImage 網(wǎng)絡(luò)獲取圖片
需要導(dǎo)入UIImageView+WebCache.h頭文件
[ImageView sd_setImageWithURL:[NSURL URLWithString:圖片網(wǎng)絡(luò)獲取地址] placeholderImage:[占位圖]];
獲取下載的圖片
- (id <SDWebImageOperation>)downloadImageWithURL:(NSURL *)url options:(SDWebImageDownloaderOptions)options
progress:(SDWebImageDownloaderProgressBlock)progressBlock
completed:(SDWebImageDownloaderCompletedBlock)completedBlock;
SDWebImage 清除圖片緩存
導(dǎo)入頭文件#import <SDImageCache.h>
// 獲取緩存信息
// 獲取緩存的圖片數(shù)量
NSInteger imageCount = [[SDImageCache sharedImageCache] getDiskCount];
// 獲取緩存的圖片大小
NSUInteger imageSize = [[SDImageCache sharedImageCache] getSize]; // 單位:字節(jié)
NSString * msg = [NSString stringWithFormat:@"緩存文件個數(shù):%ld,大小:%.2fM界牡,是否清除簿寂?", imageCount, (imageSize/1024.0)/1024.0];
// 清除緩存
UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"清除緩存" message:msg preferredStyle:UIAlertControllerStyleAlert];
// 添加AlertAction
// 取消按鈕
UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
[alertController dismissViewControllerAnimated:YES completion:nil];
}];
// 清除按鈕
UIAlertAction * okAction = [UIAlertAction actionWithTitle:@"清除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * action) {
// 清除緩存
[[SDImageCache sharedImageCache] clearDisk];
[[SDImageCache sharedImageCache] clearMemory];
// 清除已過期的圖片
[[SDImageCache sharedImageCache] cleanDisk];
}];
// 將Action添加到AlertController中
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
內(nèi)容警告清除緩存
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
// 清除內(nèi)存緩存
[[SDWebImageManager sharedManager].imageCache clearMemory];
// 取消所有下載
[[SDWebImageManager sharedManager] cancelAll];
}