#import "RootViewController.h"
#import "Cat.h"
//多个协议之间用逗号隔开
@interface RootViewController () <UITableViewDataSource, UITableViewDelegate> {
UITableView *myTableView ;
}
@property (nonatomic, retain)NSMutableArray *catArray;//因为要删除, 设置成可变的
@end
static NSString *identifier = @"abc";
@implementation RootViewController
- (void)dealloc
{
[_catArray release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// self.view.backgroundColor = [UIColor yellowColor];
myTableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
[self.view addSubview:myTableView];
myTableView.dataSource = self;//提供数据,
myTableView.delegate = self;//提供样式, 时刻
[myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identifier];
[myTableView release];
//从 plist 文件读取内容
NSString *path = [[NSBundle mainBundle] pathForResource:@"Cat" ofType:@"plist"];
NSArray *array = [NSArray arrayWithContentsOfFile:path];//最外层是数组
NSLog(@"%@", array);
//数组里面放字典(对象)
//数据封装
self.catArray = [NSMutableArray arrayWithCapacity:0];//数组初始化
for (NSDictionary *dic in array) {//得到的数组里面都是字典
//1和4比较常用
//1.属性
// Cat *cat = [[Cat alloc] init];
// cat.name = dic[@"name"];
// cat.type = dic[@"type"];
// [self.catArray addObject:cat];
// [cat release];//数组已经保留一份了
//2.KVC(间接访问属性)
// Cat *cat = [[Cat alloc] init];
// [cat setValue:dic[@"name"] forKey:@"name"];
// [cat setValue:dic[@"type"] forKey:@"type"];
// [self.catArray addObject:cat];
// [cat release];//数组已经保留一份了
//3.KVC(多个赋值)(常用), key 值和属性名相同
// Cat *cat = [[Cat alloc] init];
// [cat setValuesForKeysWithDictionary:dic];
// [self.catArray addObject:cat];
// [cat release];//数组已经保留一份了
//4.
Cat *cat = [[Cat alloc] initWithDictionary:dic];// cat 类中分装了初始化字典的方法
[self.catArray addObject:cat];
[cat release];
}
NSLog(@"%@", self.catArray);
self.navigationItem.title = @"猫咪大全";
self.navigationItem.rightBarButtonItem = self.editButtonItem;
//tableView编辑的步骤:
//1.让 tabelView进入编辑状态
//2.指定哪些行, 进入编辑(默认全选), 可选的
//3.指定编辑的样式(默认是删除), 可选的
//4.编辑完成
}
//导航栏按钮,已经封装好了
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
//继承一下父类的
[super setEditing:editing animated:animated];//切换
NSLog(@"%d", editing);//进入编辑1; 退出编辑0
//第一步: setEdit
[myTableView setEditing:editing animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.catArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
Cat *cat = self.catArray[indexPath.row];//字典赋给对象
cell.textLabel.text = [NSString stringWithFormat:@"匿名: %@, 品种:%@",cat.name, cat.type];
return cell;
}
//第二步,canEdit
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {//第一行, 不进入编辑
return NO;
}
return YES;
}
//第四步,commitEditingStyle
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%s", __FUNCTION__);
if (editingStyle == UITableViewCellEditingStyleDelete) {
//删除(原则:先删数据, 再删 cell)
[self.catArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
//添加
}
}
#pragma mark - UITableViewDelegate
//第三步,editingStyle
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == self.catArray.count - 1) {
return UITableViewCellEditingStyleInsert;
}
return UITableViewCellEditingStyleDelete;
}
@end
//在 cat 里面写一个,init 方法(字典)
#import "Cat.h"
@implementation Cat
- (void)dealloc
{
[_name release];
[_type release];
[super dealloc];
}
- (instancetype)initWithName:(NSString *)name type:(NSString *)type {
if (self = [super init]) {//将父类初始化值, 赋给子类自己
self.name = name;
self.type = type;
}
return self;
}
+ (instancetype)catWithName:(NSString *)name type:(NSString *)type {
return [[[self alloc] initWithName:name type:type] autorelease];
}
- (instancetype)initWithDictionary:(NSDictionary *)otherDictionary {
if (self = [super init]) {
[self setValuesForKeysWithDictionary:otherDictionary];
}
return self;
}
//使用 KVC 赋值时,遇到没有定义的 key值
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
}
//使用 KVC 取值时,遇到没有定义的 key值
- (id)valueForUndefinedKey:(NSString *)key {
return nil;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"name:%@, type:%@", _name, _type];
}
@end
UITabelViewEdit
最新推荐文章于 2016-03-11 08:22:29 发布
