## 一:效果
沒有圖文混排,也沒有復雜的UI,僅僅是簡單的顯示出微博數據,主要介紹AFNetworking和SDWebImage的簡單用法

## 二:加載數據AFNetworking
### AFNetworking用法
AFNetworking的用法大體有三步:?
一:下載第三方框架(githup也好,百度也好,多的是)?
二:導入頭文件?`#import "AFNetworking.h"`?
三:開始寫代碼(以上兩步所有的第三方框架都通用)
1,請求管理者
~~~
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
~~~
2,拼接請求參數
~~~
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"*****"] = @"*****";
params[@"*****"] = @"*****";
params[@"*****"] = @"*****";
。。。。
///可以寫很多參數
~~~
3,發送請求
~~~
[mgr GET:@"https:請求的網址" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//這里寫請求成功用的代碼
NSLog(@"請求成功 --- %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//這里寫請求失敗用的代碼
NSLog(@"請求失敗 --- %@",error);
}];
~~~
下面貼出微博項目中的代碼
### 加載最新的微博數據
~~~
/**
* //加載最新的微博數據
*
*
*/
-(void)loadNewStatus
{
//1,請求管理者
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
//2,拼接請求參數
NSMutableDictionary *params = [NSMutableDictionary dictionary];
HWAccount *account = [HWAccountTool account];
params[@"access_token"] = account.access_token;
// params[@"count"] = @20;
//3,發送請求
[mgr GET:@"https://api.weibo.com/2/statuses/friends_timeline.json" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
HWLog(@"請求成功 --- %@", responseObject);
//取得微博數組
self.statuses = responseObject[@"statuses"];
//刷新表格
[self.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
HWLog(@"請求失敗 --- %@",error);
}];
}
~~~
## 三:SDWebImage用法
SDWebImage的用法大體有三步:?
一:下載第三方框架(githup也好,百度也好,多的是)?
二:導入頭文件?`#import "UIImageView+WebCache.h" (這里面很多頭文件,看自己具體需要那種了)`?
三:開始寫代碼(以上兩步所有的第三方框架都通用)?
這個代碼寫起來就更簡單了,例如微博中我們想要讓他自己下載緩存一張圖片用作每個tableViewCell的圖片,并且顯示一張占位圖片,一句代碼就搞定了
~~~
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:placehoder];
~~~
然后需要做防止程序內存溢出的操作?
一:在程序AppDelegate 中寫入頭文件`#import "SDWebImageManager.h"`?
二:調用方法,在整個程序內存警報時候調用`-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application?
`
三:方法內寫入
~~~
//整個程序內存警報時候調用
-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
SDWebImageManager *mgr = [SDWebImageManager sharedManager];
//1,取消下載
[mgr cancelAll];
//2,清除內存中的所有圖片
[mgr.imageCache clearMemory];
}
~~~
這里貼出cell全部代碼供參考
~~~
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"status";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
//用indexPathRow取出對應的一條微博字典
NSDictionary *status = self.statuses[indexPath.row];
//設置微博作者
NSDictionary *user = status[@"user"];
cell.textLabel.text = user[@"name"];
//設置微博內容
cell.detailTextLabel.text = status[@"text"];
//設置微博頭像
NSString *imageUrl = user[@"profile_image_url"];
//占位圖
UIImage *placehoder = [UIImage imageNamed:@"avatar_default_small"];
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:placehoder];
return cell;
}
~~~
- 前言
- (1)微博主框架-子控制器的添加
- (2)微博主框架-自定義導航控制器NavigationController
- (3)微博主框架-UIImage防止iOS7之后自動渲染_定義分類
- (4)微博自定義tabBar中間的添加按鈕
- (5)微博自定義搜索框searchBar
- (6)導航控制器NavigationController 的滑動回退功能實現
- (7)程序啟動新特性用UICollectionViewController實現
- (8)用AFNetworking和SDWebImage簡單加載微博數據
- (9)微博模型之時間相關重要操作,判斷剛剛,昨天,今年等等
- (10)微博cell中圖片的顯示以及各種填充模式簡介
- (11)發送微博自定義TextView實現帶占位文字
- (12)發送微博自定義工具條代理實現點擊事件
- (13)發送微博調用相機里面的圖片以及調用相機