//
// RootTableViewController.m
// KVO_
观察数组
//
//
//
// Created by 王聪 on 14/8/13.
// Copyright (c) 2014
年
Congwang. All rights reserved.
//
#import "RootTableViewController.h"
#import "Stock.h"
@interface RootTableViewController ()
// 数组属性
@property ( nonatomic , strong ) NSMutableArray *dataSource;
@end
@implementation RootTableViewController
- ( void )dealloc{
//
移除观察者
[
self
removeObserver
:
self
forKeyPath
:
@"dataSource"
context
:
NULL
];
}
- ( void )viewDidLoad {
[
super
viewDidLoad
];
//
数组初始化
self
.
dataSource
= [
NSMutableArray
array
];
//
当前对象观察当前对象
dataSource
属性的变化
[
self
addObserver
:
self
forKeyPath
:
@"dataSource"
options
:
NSKeyValueObservingOptio nOld
context
:
NULL
];
//
UIBarButtonItem
*right = [[
UIBarButtonItem
alloc
]
initWithBarButtonSystemI tem
:
UIBarButtonSystemItemAdd
target
:
self
action
:
@selector
(addSomething:)];
self
.
navigationItem
.
rightBarButtonItem
= right;
//
注册
cell
[
self
.
tableView
registerClass
:[
UITableViewCell
class
]
forCellReuseIdentifier
:
@"cell"
];
}
- ( void )addSomething:( UIBarButtonItem *)sender{
Stock
*stock = [[
Stock
alloc
]
init
];
stock.
stockName
=
@"
中石化
"
;
int
a =
arc4random
() %
1000
+
1
;
stock.
price
= a /
1.1
;
[
self
insertObject
:stock
inDataSourceAtIndex
:
self
.
dataSource
.
count
];
}
- ( void )observeValueForKeyPath:( NSString *)keyPath ofObject:( id )object change:( NSDictionary *)change context:( void *)context{
if
([change[
@"kind"
]
integerValue
] ==
NSKeyValueChangeInsertio n
) {
//
插入单元格
NSIndexPath
*indexPath = [
NSIndexPath
indexPathForRow
:
self
.
dataSource
.
count
-
1
inSection
:
0
];
//
第一个参数
,
插入单元格的
indexPath
//
二
:
插入时的动画
[
self
.
tableView
insertRowsAtIndexPaths
:
@[
indexPath
]
withRowAnimation
:
UITableViewRowAnimationL eft
];
}
else
if
([change[
@"kind"
]
integerValue
] ==
NSKeyValueChangeRemoval
){
NSIndexSet
*set = change[
@"indexes"
];
//
枚举集合中的每一个值
(
当前
set
中永远为一个
)
__block
NSIndexPath
*indexPath =
nil
;
[set
enumerateIndexesUsingBlo ck
:^(
NSUInteger
idx,
BOOL
*stop) {
indexPath = [
NSIndexPath
indexPathForRow
:idx
inSection
:
0
];
}];
//
删除单元格
[
self
.
tableView
deleteRowsAtIndexPaths
:
@[
indexPath
]
withRowAnimation
:
UITableViewRowAnimationF ade
];
}
else
if
([change[
@"kind"
]
integerValue
] ==
NSKeyValueChangeReplacem ent
){
NSIndexSet
*set = change[
@"indexes"
];
//
枚举集合中的每一个值
(
当前
set
中永远为一个
)
__block
NSIndexPath
*indexPath =
nil
;
[set
enumerateIndexesUsingBlo ck
:^(
NSUInteger
idx,
BOOL
*stop) {
indexPath = [
NSIndexPath
indexPathForRow
:idx
inSection
:
0
];
}];
//
更新单元格
[
self
.
tableView
reloadRowsAtIndexPaths
:
@[
indexPath
]
withRowAnimation
:
UITableViewRowAnimationF ade
];
}
}
//* 当我们观察 数组的变化时一定会要完成系统为我们生成的两个方法 , 一个是 insert, 一个是 remove, 必须配对出现 , 在该方法中完成数组的对应操作
- ( void )insertObject:( id )object inDataSourceAtIndex:( NSUInteger )index{
//
向数组中插入数据
[
self
.
dataSource
insertObject
:object
atIndex
:index];
}
// 系统动态生成的方法
- ( void )removeObjectFromDataSour ceAtIndex:(
NSUInteger
)index{
[
self
.
dataSource
removeObjectAtIndex
:index];
}
// 系统为我们生成了一个修改方法
- ( void )replaceObjectInDataSourc eAtIndex:(
NSUInteger
)index withObject:(
id
)object{
//
在该方法中修改数组的元素
[
self
.
dataSource
replaceObjectAtIndex
:index
withObject
:object];
}
- ( void )didReceiveMemoryWarning {
[
super
didReceiveMemoryWarning
];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- ( NSInteger )numberOfSectionsInTableV iew:(
UITableView
*)tableView {
return
1
;
}
- ( NSInteger )tableView:( UITableView *)tableView numberOfRowsInSection:( NSInteger )section {
return
self
.
dataSource
.
count
;
}
- ( UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath {
UITableViewCell
*cell = [tableView
dequeueReusableCellWithI dentifier
:
@"cell"
forIndexPath
:indexPath];
//
取出模型
Stock
*stock =
self
.
dataSource
[indexPath.
row
];
cell.
textLabel
.
textColor
= [
UIColor
colorWithRed
:
arc4random
()%
256
/
255.0
green
:
arc4random
()%
256
/
255.0
blue
:
arc4random
()%
256
/
255.0
alpha
:
1
];
cell.
textLabel
.
text
= [
NSString
stringWithFormat
:
@"%@ -- %f"
, stock.
stockName
, stock.
price
];
return
cell;
}
- ( void )tableView:( UITableView *)tableView didSelectRowAtIndexPath:( NSIndexPath *)indexPath{
//
点击
cell
的时候修改对应
cell
的内容
//1.
先修改数据源
//
先找到模型
Stock
*stock =
self
.
dataSource
[indexPath.
row
];
stock.
stockName
=
@"
中石油
"
;
[
self
replaceObjectInDataSourc eAtIndex
:indexPath.
row
withObject
:stock];
}
// Override to support conditional editing of the table view.
- ( BOOL )tableView:( UITableView *)tableView canEditRowAtIndexPath:( NSIndexPath *)indexPath {
return
YES
;
}
// Override to support editing the table view.
- ( void )tableView:( UITableView *)tableView commitEditingStyle:( UITableViewCellEditingSt yle
)editingStyle forRowAtIndexPath:(
NSIndexPath
*)indexPath {
//
删除
if
(editingStyle ==
UITableViewCellEditingSt yleDelete
) {
//
删除数据源
[
self
removeObjectFromDataSour ceAtIndex
:indexPath.
row
];
}
//
插入
else
if
(editingStyle ==
UITableViewCellEditingSt yleInsert
) {
}
}
#import "RootTableViewController.h"
#import "Stock.h"
@interface RootTableViewController ()
// 数组属性
@property ( nonatomic , strong ) NSMutableArray *dataSource;
@end
@implementation RootTableViewController
- ( void )dealloc{
}
- ( void )viewDidLoad {
}
- ( void )addSomething:( UIBarButtonItem *)sender{
}
- ( void )observeValueForKeyPath:( NSString *)keyPath ofObject:( id )object change:( NSDictionary *)change context:( void *)context{
}
//* 当我们观察 数组的变化时一定会要完成系统为我们生成的两个方法 , 一个是 insert, 一个是 remove, 必须配对出现 , 在该方法中完成数组的对应操作
- ( void )insertObject:( id )object inDataSourceAtIndex:( NSUInteger )index{
}
// 系统动态生成的方法
- ( void )removeObjectFromDataSour
}
// 系统为我们生成了一个修改方法
- ( void )replaceObjectInDataSourc
}
- ( void )didReceiveMemoryWarning {
}
#pragma mark - Table view data source
- ( NSInteger )numberOfSectionsInTableV
}
- ( NSInteger )tableView:( UITableView *)tableView numberOfRowsInSection:( NSInteger )section {
}
- ( UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath {
}
- ( void )tableView:( UITableView *)tableView didSelectRowAtIndexPath:( NSIndexPath *)indexPath{
}
// Override to support conditional editing of the table view.
- ( BOOL )tableView:( UITableView *)tableView canEditRowAtIndexPath:( NSIndexPath *)indexPath {
}
// Override to support editing the table view.
- ( void )tableView:( UITableView *)tableView commitEditingStyle:( UITableViewCellEditingSt
}
@end
Stock.h
//
// Stock.h
// KVC_观察数组
//
//
//
// Created by 王聪 on 14/8/13.
// Copyright (c) 2014
年
Congwang. All rights reserved.
//
#import
@interface Stock : NSObject
//股票的名字
@property (nonatomic, copy)NSString *stockName;
@property (nonatomic, assign)float price;//股票的价格
@end
#import
@interface Stock : NSObject
//股票的名字
@property (nonatomic, copy)NSString *stockName;
@property (nonatomic, assign)float price;//股票的价格
@end