IOS_UI_day7_UITableView_优化

H:/IOS_UI/day7-00-UITableView总结笔记.m
一、UITableView的代理方法
#pragma mark 每一行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

#pragma mark 选中了某一行就会调用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

#pragma mark 取消选中了某一行就会调用
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

#pragma mark 当用户提交了一个编辑操作就会调用(比如点击了“删除”按钮)
// 只要实现了这个方法,就会默认添加滑动删除功能
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

#pragma mark 当移动了某一行cell就会调用
// 只要实现了这个方法,就会默认添加排序功能
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

二、修改Cell的状态
1.最好通过“修改模型数据”来修改Cell的状态

2.修改步骤
1> 修改模型数据
2> 刷新表格
* 整体刷新:reloadData(最重要)
* 局部刷新:reloadRowsAtIndexPaths:withRowAnimation:

三、UITableView常见方法
1.取消选中某一行(去掉cell选中时默认的蓝色背景)
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;

2.局部刷新(仅仅刷新indexPaths数组中装着的行)
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

3.整体刷新(屏幕中的每一行都刷新)
- (void)reloadData;

4.直接删除界面上的行数(要求模型数据也要删掉对应的数量)
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

5.设置编辑模式
@property(nonatomic,getter=isEditing) BOOL editing; 
- (void)setEditing:(BOOL)editing animated:(BOOL)animated;

// 注意:
不管是局部刷新,还是整体刷新,原理都是:
UITableView重新向数据源(dataSource)和代理(delegate)发送相应的消息,最终将得到的数据展示出来

H:/IOS_UI/day7-01-UITableView-性能优化的应用-MJViewController.h
//
//  MJViewController.h
//  01-UITableView01-性能优化的应用
//
//  Created by apple on 13-11-30.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MJViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *removeItem;

- (IBAction)remove;

@end

H:/IOS_UI/day7-01-UITableView-性能优化的应用-MJViewController.m
//  MJViewController.m
//  01-UITableView01-性能优化的应用
//  Created by apple on 13-11-30.
//  Copyright (c) 2013年 itcast. All rights reserved.
#import "MJViewController.h"
#import "Shop.h"
@interface MJViewController ()  <UITableViewDataSource, UITableViewDelegate>
{
    NSMutableArray *_shops;
    NSMutableArray *_deleteShops; // 存放即将要删除的模型数据
}
@end
@implementation MJViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    // 1.加载plist,array中装的每个元素,都是NSDictionary对象
    NSString *path = [[NSBundle mainBundle] pathForResource:@"shops.plist" ofType:nil];
	//全路径
    NSArray *array = [NSArray arrayWithContentsOfFile:path];    
    _shops = [NSMutableArray array];
	_deleteShops = [NSMutableArray array];
	// 2.将array中的字典元素填充到Shop模型对象,并将Shop模型对象添加到可变数组
    for (NSDictionary *dict in array) {
        // 2.1.创建模型
//        Shop *s = [[Shop alloc] initWithDict:dict];
        Shop *s = [Shop shopWithDict:dict];
        // 2.2.并将Shop模型对象添加到可变数组
        [_shops addObject:s];
    }
}
#pragma mark 删除数据
- (void)remove
{
    // 0.获得所有要被删除的数据的行号集合
    NSMutableArray *deletePaths = [NSMutableArray array];
	//遍历,欲被删除的对象数组
    for (Shop *s in _deleteShops) {
		//找出欲被删除的对象数组中的元素,在主数组中的索引,即为界面上的行号
        int row = [_shops indexOfObject:s];
		//根据行号,创建NSIndexPath对象
        NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0];
		//添加到要被删除的NSIndexPath数组中
        [deletePaths addObject:path];
    }
    // 1.首先,删除主模型数组中的数据(将_deleteShops中的所有对象元素从_shops中删除)
    [_shops removeObjectsInArray:_deleteShops];
    // 2.然后,再刷新表格,整体,或局部刷新均可
//    [self.tableView reloadData];
    [self.tableView deleteRowsAtIndexPaths:deletePaths withRowAnimation:UITableViewRowAnimationTop];
    // 3.更改标题
    _titleLabel.text = @"淘宝";
    // 4.最后清空垃圾筒,即数组_deleteShops(清空已经删除的数据)
    [_deleteShops removeAllObjects];
    // 5.禁止删除按钮被人点击
    _removeItem.enabled = NO;
}
#pragma mark - 数据源方法
#pragma mark 多少行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _shops.count;
}
#pragma mark 每一行显示怎样的cell(内容)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.定义一个可重用标识
    static NSString *ID = @"cell";
    // 2.去缓存池中取出可循环利用的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    // 3.如果缓存中没有可循环利用的cell,创建新cell,并记得指定标识
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    // 4.设置每个cell独一无二的商品数据
    // 4.0.取出indexPath这行对应的商品shop模型对象
    Shop *s = _shops[indexPath.row];
    // 4.1.独一无二的商品名称
    cell.textLabel.text = s.name;
    // 4.2.独一无二的商品描述
    cell.detailTextLabel.text = s.desc;
    // 4.3.独一无二的商品图片
    cell.imageView.image = [UIImage imageNamed:s.icon];
    // 4.4.检测打钩状态
    // 如果_deleteShops数组中包含了s这个模型对象
    if ([_deleteShops containsObject:s]) { // 需要打钩
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else { // 不需要打钩
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}
#pragma mark - 代理方法
#pragma mark 每一行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}
#pragma mark 监听cell的点击
#pragma mark 选中了某一行就会调用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// 每个cell都有选中状态(selected)
    // 家居 --> 未选中 selected = NO
    // 美食 --> 选中 selected = YES
    // 0.取消选中这一行(去掉cell默认的蓝色背景)
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    // 1.控制当前行的数据是否需要选中
    Shop *s = _shops[indexPath.row];
	// 如果之前是选中,现在再点击,即认识是:取消选中
    if ([_deleteShops containsObject:s]) { 
        [_deleteShops removeObject:s];
    } else { 
        // 反之之前从未选中过,这此点击是选中,即要删除该条row
        [_deleteShops addObject:s];
    }
    // 2.局部刷新表格,刷新指定IndexPaths
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
	// 3.显示标题
    if (_deleteShops.count == 0) {
        _titleLabel.text = @"淘宝";
        _removeItem.enabled = NO;
    } else {
        _removeItem.enabled = YES;
        _titleLabel.text = [NSString stringWithFormat:@"淘宝(%d)", _deleteShops.count];
    }
//    for (Shop *shop in _deleteShops) {
//        NSLog(@"%@", shop.name);
//    }
//    [tableView reloadData];
    // 1.取出indexPath这行对应的cell
//    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
//    selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
#pragma mark 取消选中了某一行就会调用
//- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//}
@end

H:/IOS_UI/day7-01-UITableView-性能优化的应用-Shop.h
//
//  Shop.h
//  01-UITableView01-性能优化的应用
//
//  Created by apple on 13-11-30.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Shop : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *desc;

- (id)initWithDict:(NSDictionary *)dict;
+ (id)shopWithDict:(NSDictionary *)dict;
@end

H:/IOS_UI/day7-01-UITableView-性能优化的应用-Shop.m
//
//  Shop.m
//  01-UITableView01-性能优化的应用
//
//  Created by apple on 13-11-30.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "Shop.h"

@implementation Shop
- (id)initWithDict:(NSDictionary *)dict
{
    if (self = [super init]) {
        // 将字典中的键值对转成模型属性
        self.name = dict[@"name"];
        self.icon = dict[@"icon"];
        self.desc = dict[@"desc"];
    }
    return self;
}

+ (id)shopWithDict:(NSDictionary *)dict
{
//    Shop *s = [[Shop alloc] init];
//    s.name = dict[@"name"];
//    s.icon = dict[@"icon"];
//    s.desc = dict[@"desc"];
    return [[self alloc] initWithDict:dict];
}
@end

H:/IOS_UI/day7-01-UITableView-性能优化的应用-shops.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
	<dict>
		<key>desc</key>
		<string>休闲零食,酒水饮料,进口食品,地方特产</string>
		<key>icon</key>
		<string>food.jpg</string>
		<key>name</key>
		<string>美食</string>
	</dict>
	<dict>
		<key>desc</key>
		<string>模型玩具,积木拼插,娃娃玩具,电动玩具</string>
		<key>icon</key>
		<string>toy.jpg</string>
		<key>name</key>
		<string>玩具</string>
	</dict>
	<dict>
		<key>desc</key>
		<string>清洁剂,纸制品,清洁用具,一次性用品</string>
		<key>icon</key>
		<string>home.jpg</string>
		<key>name</key>
		<string>家居</string>
	</dict>
	<dict>
		<key>desc</key>
		<string>电脑整机,打印机,数码影音,数码影像</string>
		<key>icon</key>
		<string>mechine.jpg</string>
		<key>name</key>
		<string>生活电器</string>
	</dict>
	<dict>
		<key>desc</key>
		<string>小说,文艺,教育,科技,杂志,社科</string>
		<key>icon</key>
		<string>book.jpg</string>
		<key>name</key>
		<string>音像图书</string>
	</dict>
	<dict>
		<key>desc</key>
		<string>女包,男包,旅行箱包,钱包卡套,儿童箱包,腰包</string>
		<key>icon</key>
		<string>bag.jpg</string>
		<key>name</key>
		<string>箱包</string>
	</dict>
	<dict>
		<key>desc</key>
		<string>精品女装,品质男装,童装童鞋,运动户外,鞋靴</string>
		<key>icon</key>
		<string>clothe.jpg</string>
		<key>name</key>
		<string>服装</string>
	</dict>
	<dict>
		<key>desc</key>
		<string>手机充值,鲜花速递,旅游服务,教育培训,摄像服务</string>
		<key>icon</key>
		<string>life.jpg</string>
		<key>name</key>
		<string>生活服务</string>
	</dict>
	<dict>
		<key>desc</key>
		<string>哺育喂养,洗护清洁,孕妈专区,婴幼家纺,宝宝服饰</string>
		<key>icon</key>
		<string>baby.jpg</string>
		<key>name</key>
		<string>母婴用品</string>
	</dict>
	<dict>
		<key>desc</key>
		<string>女性护理,口腔护理,沐浴用品,男士护理,魅力香氛,美容工具,精油芳疗</string>
		<key>icon</key>
		<string>buty.jpg</string>
		<key>name</key>
		<string>美容化妆</string>
	</dict>
	<dict>
		<key>desc</key>
		<string>手表,钻石,翡翠玉石,黄金/k金/铂金,贵重宝石,天然珍珠</string>
		<key>icon</key>
		<string>decoration.jpg</string>
		<key>name</key>
		<string>珠宝饰品</string>
	</dict>
	<dict>
		<key>desc</key>
		<string>衣物清洁护理,家庭清洁护理,纸制品,一次性用品,清洁用具,厨具锅具,餐具水具</string>
		<key>icon</key>
		<string>clean.jpg</string>
		<key>name</key>
		<string>厨卫清洁</string>
	</dict>
	<dict>
		<key>desc</key>
		<string>手机通讯,运营商,手机配件,数码影像,时尚影音,数码配件</string>
		<key>icon</key>
		<string>phone.jpg</string>
		<key>name</key>
		<string>手机数码</string>
	</dict>
	<dict>
		<key>desc</key>
		<string>炒锅,汤锅,平底锅,套装锅,蒸锅,奶锅,煲,压力锅</string>
		<key>icon</key>
		<string>knife.jpg</string>
		<key>name</key>
		<string>锅具餐具</string>
	</dict>
	<dict>
		<key>desc</key>
		<string>宠物日用品,宠物医护美容,出行装备,宠物玩具,水族宠物</string>
		<key>icon</key>
		<string>pet.jpg</string>
		<key>name</key>
		<string>宠物园艺</string>
	</dict>
</array>
</plist>

H:/IOS_UI/day7-02-UITableView-性能优化的应用-MJViewController.h
//
//  MJViewController.h
//  01-UITableView01-性能优化的应用
//
//  Created by apple on 13-11-30.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MJViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *removeItem;

- (IBAction)remove;

@end

H:/IOS_UI/day7-02-UITableView-性能优化的应用-MJViewController.m
//  MJViewController.m
//  01-UITableView01-性能优化的应用
//  Created by apple on 13-11-30.
//  Copyright (c) 2013年 itcast. All rights reserved.
#import "MJViewController.h"
#import "Shop.h"
@interface MJViewController ()  <UITableViewDataSource, UITableViewDelegate>
{
    NSMutableArray *_shops;
    NSMutableArray *_deleteShops; // 存放即将要删除的模型数据
}
@end
@implementation MJViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    // 1.加载plist,array中装的都是NSDictionary对象
    NSString *path = [[NSBundle mainBundle] pathForResource:@"shops" ofType:@"plist"];
	//全路径
    NSArray *array = [NSArray arrayWithContentsOfFile:path];
    _shops = [NSMutableArray array];
	_deleteShops = [NSMutableArray array];
	// 2.将array中的字典元素填充到Shop模型对象,并将Shop模型对象添加到可变数组
    for (NSDictionary *dict in array) {
        // 2.1.创建模型
//        Shop *s = [[Shop alloc] initWithDict:dict];
        Shop *s = [Shop shopWithDict:dict];
        // 2.2.将模型对象放进数组中
        [_shops addObject:s];
    }
}
#pragma mark 删除数据
- (void)remove
{
    // 0.获得所有要被删除的数据的行号集合
    NSMutableArray *deletePaths = [NSMutableArray array];
	//遍历,欲被删除的对象数组
    for (Shop *s in _deleteShops) {
		//找出欲被删除的对象数组中的元素,在主数组中的索引,即为界面上的行号
        int row = [_shops indexOfObject:s];
		//根据行号,创建NSIndexPath对象
        NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0];
		//添加NSIndexPath对象到要被删除的NSIndexPath数组中
        [deletePaths addObject:path];
    }
    // 1.首先,从主模型数组中的删除要删除的数据(将_deleteShops中的所有对象元素从_shops中删除)
    [_shops removeObjectsInArray:_deleteShops];
    // 2.然后清空垃圾筒,即数组_deleteShops(清空已经删除的数据)
    [_deleteShops removeAllObjects];
    // 3.最后调用tableView的deleteRowsAtIndexPaths方法删除特定的这些rows
    [self.tableView deleteRowsAtIndexPaths:deletePaths withRowAnimation:UITableViewRowAnimationTop];
}
#pragma mark - 数据源方法
#pragma mark 每次都会调用,返回共多少行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_deleteShops.count == 0) {
        _titleLabel.text = @"淘宝";
        _removeItem.enabled = NO;
    } else {
        _removeItem.enabled = YES;
        _titleLabel.text = [NSString stringWithFormat:@"淘宝(%d)", _deleteShops.count];
    }
    return _shops.count;
}
#pragma mark 只要有一行要进入视线范围,就被调用,每一行显示怎样的cell(内容)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.定义一个可重用标识
    static NSString *ID = @"cell";
    // 2.去缓存池中取出可循环利用的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    // 3.如果缓存中没有可循环利用的cell,创建新cell,并记得指定标识
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    // 4.设置每个cell独一无二的商品数据
    // 4.0.取出indexPath这行对应的商品shop模型对象
    Shop *s = _shops[indexPath.row];
    // 4.1.独一无二的商品名称
    cell.textLabel.text = s.name;
    // 4.2.独一无二的商品描述
    cell.detailTextLabel.text = s.desc;
    // 4.3.独一无二的商品图片
    cell.imageView.image = [UIImage imageNamed:s.icon];
    // 4.4.显示cell之前,先检测是否需要进行打钩
    // 如果_deleteShops数组中包含了s这个数据对象
    if ([_deleteShops containsObject:s]) { // 出产前,需要打钩
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else { // 不需要打钩
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}
#pragma mark - 代理方法
#pragma mark 每一行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}
#pragma mark 监听cell的点击
#pragma mark 选中了某一行就会调用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 0.动画取消当前选中行的高亮背景蓝色(去掉cell默认的蓝色背景)
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    // 1.从主数组中取出当前点击行号对应的数据对象
    Shop *s = _shops[indexPath.row];
	// 如果删除数组,包含了该数据对象,说明,之前是选中过,现在取消选中
    if ([_deleteShops containsObject:s]) { 
        // 从删除列表中移除
        [_deleteShops removeObject:s];
    } else { // 之前是没有选中过,现在选中
        // 添加删除列表中
        [_deleteShops addObject:s];
    }
    // 2.局部刷新表格,也可以[tableView reloadData]
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
}
@end

H:/IOS_UI/day7-02-UITableView-性能优化的应用-Shop.h
//
//  Shop.h
//  01-UITableView01-性能优化的应用
//
//  Created by apple on 13-11-30.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Shop : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *desc;

- (id)initWithDict:(NSDictionary *)dict;
+ (id)shopWithDict:(NSDictionary *)dict;
@end

H:/IOS_UI/day7-02-UITableView-性能优化的应用-Shop.m
//
//  Shop.m
//  01-UITableView01-性能优化的应用
//
//  Created by apple on 13-11-30.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "Shop.h"

@implementation Shop
- (id)initWithDict:(NSDictionary *)dict
{
    if (self = [super init]) {
        // 将字典中的键值对转成模型属性
        self.name = dict[@"name"];
        self.icon = dict[@"icon"];
        self.desc = dict[@"desc"];
    }
    return self;
}

+ (id)shopWithDict:(NSDictionary *)dict
{
//    Shop *s = [[Shop alloc] init];
//    s.name = dict[@"name"];
//    s.icon = dict[@"icon"];
//    s.desc = dict[@"desc"];
    return [[self alloc] initWithDict:dict];
}
@end

H:/IOS_UI/day7-02-UITableView-性能优化的应用-shops.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
	<dict>
		<key>desc</key>
		<string>休闲零食,酒水饮料,进口食品,地方特产</string>
		<key>icon</key>
		<string>food.jpg</string>
		<key>name</key>
		<string>美食</string>
	</dict>
	<dict>
		<key>desc</key>
		<string>模型玩具,积木拼插,娃娃玩具,电动玩具</string>
		<key>icon</key>
		<string>toy.jpg</string>
		<key>name</key>
		<string>玩具</string>
	</dict>
	<dict>
		<key>desc</key>
		<string>清洁剂,纸制品,清洁用具,一次性用品</string>
		<key>icon</key>
		<string>home.jpg</string>
		<key>name</key>
		<string>家居</string>
	</dict>
	<dict>
		<key>desc</key>
		<string>电脑整机,打印机,数码影音,数码影像</string>
		<key>icon</key>
		<string>mechine.jpg</string>
		<key>name</key>
		<string>生活电器</string>
	</dict>
	<dict>
		<key>desc</key>
		<string>小说,文艺,教育,科技,杂志,社科</string>
		<key>icon</key>
		<string>book.jpg</string>
		<key>name</key>
		<string>音像图书</string>
	</dict>
	<dict>
		<key>desc</key>
		<string>女包,男包,旅行箱包,钱包卡套,儿童箱包,腰包</string>
		<key>icon</key>
		<string>bag.jpg</string>
		<key>name</key>
		<string>箱包</string>
	</dict>
	<dict>
		<key>desc</key>
		<string>精品女装,品质男装,童装童鞋,运动户外,鞋靴</string>
		<key>icon</key>
		<string>clothe.jpg</string>
		<key>name</key>
		<string>服装</string>
	</dict>
	<dict>
		<key>desc</key>
		<string>手机充值,鲜花速递,旅游服务,教育培训,摄像服务</string>
		<key>icon</key>
		<string>life.jpg</string>
		<key>name</key>
		<string>生活服务</string>
	</dict>
	<dict>
		<key>desc</key>
		<string>哺育喂养,洗护清洁,孕妈专区,婴幼家纺,宝宝服饰</string>
		<key>icon</key>
		<string>baby.jpg</string>
		<key>name</key>
		<string>母婴用品</string>
	</dict>
	<dict>
		<key>desc</key>
		<string>女性护理,口腔护理,沐浴用品,男士护理,魅力香氛,美容工具,精油芳疗</string>
		<key>icon</key>
		<string>buty.jpg</string>
		<key>name</key>
		<string>美容化妆</string>
	</dict>
	<dict>
		<key>desc</key>
		<string>手表,钻石,翡翠玉石,黄金/k金/铂金,贵重宝石,天然珍珠</string>
		<key>icon</key>
		<string>decoration.jpg</string>
		<key>name</key>
		<string>珠宝饰品</string>
	</dict>
	<dict>
		<key>desc</key>
		<string>衣物清洁护理,家庭清洁护理,纸制品,一次性用品,清洁用具,厨具锅具,餐具水具</string>
		<key>icon</key>
		<string>clean.jpg</string>
		<key>name</key>
		<string>厨卫清洁</string>
	</dict>
	<dict>
		<key>desc</key>
		<string>手机通讯,运营商,手机配件,数码影像,时尚影音,数码配件</string>
		<key>icon</key>
		<string>phone.jpg</string>
		<key>name</key>
		<string>手机数码</string>
	</dict>
	<dict>
		<key>desc</key>
		<string>炒锅,汤锅,平底锅,套装锅,蒸锅,奶锅,煲,压力锅</string>
		<key>icon</key>
		<string>knife.jpg</string>
		<key>name</key>
		<string>锅具餐具</string>
	</dict>
	<dict>
		<key>desc</key>
		<string>宠物日用品,宠物医护美容,出行装备,宠物玩具,水族宠物</string>
		<key>icon</key>
		<string>pet.jpg</string>
		<key>name</key>
		<string>宠物园艺</string>
	</dict>
</array>
</plist>

H:/IOS_UI/day7-03-UITableView-删除数据-MJViewController.h
//
//  MJViewController.h
//  02-UITableView02-删除数据
//
//  Created by apple on 13-11-30.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MJViewController : UIViewController
- (IBAction)remove:(id)sender;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

H:/IOS_UI/day7-03-UITableView-删除数据-MJViewController.m
//
//  MJViewController.m
//  02-UITableView02-删除数据
//
//  Created by apple on 13-11-30.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "MJViewController.h"
#import "Person.h"

@interface MJViewController () <UITableViewDataSource, UITableViewDelegate>
{
    NSMutableArray *_persons;
}
@end

@implementation MJViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _persons = [NSMutableArray array];
    for (int i = 0; i<30; i++) {
        Person *p = [[Person alloc] init];
        p.name = [NSString stringWithFormat:@"Person---%d", i];
        p.phone = [NSString stringWithFormat:@"%d", 10000 + arc4random_uniform(10000000)];\
        [_persons addObject:p];
    }
}

#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _persons.count;
}

#pragma mark 每一行显示怎样的cell(内容)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.定义一个标识
    static NSString *ID = @"cell";
    
    // 2.去缓存池中取出可循环利用的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    // 3.如果缓存中没有可循环利用的cell
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
    }
    
    // 4.设置数据
    // 4.1.取出模型
    Person *p = _persons[indexPath.row];
    
    // 4.2.姓名
    cell.textLabel.text = p.name;
    
    // 4.3.手机
    cell.detailTextLabel.text = p.phone;
    
    return cell;
}

#pragma mark - 代理方法
#pragma mark 当用户提交了一个编辑操作就会调用(比如点击了“删除”按钮)
// 只要实现了这个方法,就会默认添加滑动删除功能
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 如果不是删除操作,直接返回
    if (editingStyle != UITableViewCellEditingStyleDelete) return;
    
    // 1.删除模型数据
    [_persons removeObjectAtIndex:indexPath.row];
    
    // 2.刷新表格
//    [tableView reloadData];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}

#pragma mark 当移动了某一行cell就会调用
// 只要实现了这个方法,就会默认添加排序功能
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//    NSLog(@"%d --- %d", sourceIndexPath.row, destinationIndexPath.row);
    
//    [_persons exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
    
    // 1.取出要拖动的模型数据
    Person *p = _persons[sourceIndexPath.row];
    
    // 2.删除之前行的数据
    [_persons removeObject:p];
    
    // 3.插入数据到新的位置
    [_persons insertObject:p atIndex:destinationIndexPath.row];
}

#pragma mark 删除
- (IBAction)remove:(id)sender {
    // 1.进入编辑模式
//    self.tableView.editing = YES;
    BOOL result = !self.tableView.isEditing;
    [self.tableView setEditing:result animated:YES];
}
@end

H:/IOS_UI/day7-03-UITableView-删除数据-Person.h
//
//  Person.h
//  02-UITableView02-删除数据
//
//  Created by apple on 13-11-30.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Person : NSObject
@property (nonatomic, copy) NSString *name; // 姓名
@property (nonatomic, copy) NSString *phone; // 电话
@end

H:/IOS_UI/day7-03-UITableView-删除数据-Person.m
//
//  Person.m
//  02-UITableView02-删除数据
//
//  Created by apple on 13-11-30.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "Person.h"

@implementation Person

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值