图片异步加载
#import "RootTableViewController.h"
#import "MyTableViewCell.h"
#import "Activity.h"
#import "UIImageView+WebCache.h" //SD中的图片异步加载
@interface RootTableViewController ()
@property(nonatomic,strong)NSMutableArray *dataArr;
@end
@implementation RootTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"cell"];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self handleData];
}
- (void)handleData{
self.dataArr = [NSMutableArray array];
// 准备URL
NSURL *url = [NSURL URLWithString:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php"];
// 准备请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 异步请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSArray *arr = [dict objectForKey:@"events"];
for (NSDictionary *d in arr) {
Activity *activity = [[Activity alloc]init];
[activity setValuesForKeysWithDictionary:d];
[self.dataArr addObject:activity];
// 刷新数据
[self.tableView reloadData];
}
}];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
cell.activityImage.image = [UIImage imageNamed:@"picholder"];
cell.activityLabel.text = @"aaa";
Activity *activity = [self.dataArr objectAtIndex:indexPath.row];
cell.activityLabel.text = activity.title;
// 请求图片
NSURL *url = [NSURL URLWithString:activity.image];
// NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// cell.activityImage.image = [UIImage imageWithData:data];
//}];
// 使用第三方 SDWebImage去实现异步加载
[cell.activityImage sd_setImageWithURL:url];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return [MyTableViewCell heightForCell];
}
KVO
#import "RootViewController.h"
#import "Student.h"
@interface RootViewController ()
@property(nonatomic,strong)UIButton *button;
@property(nonatomic,strong)Student *s;
@end
@implementation RootViewController
// KVO 键值观察者 他是一种观察者的设计模式 他可以用来检测Model类对象中属性值如果有变化,可以在观察者方法里面做相应处理...
// 由于kvc的设计模式,是由Controller去控制model和View
// model和view 是不直接产生通信的,,但是有了KVO机制 让Controller作为观察者,实现观察者的回调方法,如果model的属性值发生了改变,view也可以做出相应的变化
- (void)viewDidLoad {
[super viewDidLoad];
self.button = [UIButton buttonWithType:UIButtonTypeSystem];
self.button.frame = CGRectMake(100, 100, 100, 100);
[self.button setTitle:@"KVO改值" forState:UIControlStateNormal];
[self.view addSubview:self.button];
[self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
self.s = [[Student alloc]init];
self.s.name = @"小明";
self.s.gender = @"男";
// 添加观察者
[self.s addObserver:self forKeyPath:@"gender" options:NSKeyValueObservingOptionNew context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"name改变" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil];
[alert show];
[self.s removeObserver:self forKeyPath:@"gender"];
}
- (void)buttonAction:(UIButton *)sender{
self.s.name = @"小红";
self.s.gender = @"男";
}