给局部变量加上关键字:static
作用:不管调用多少次,这个变量将只会创建一次
修改Cell的状态
最好通过“修改模型数据”来修改Cell的状态
修改步骤
修改模型数据
刷新表格
整体刷新:reloadData
局部刷新:reloadRowsAtIndexPaths:withRowAnimation:
删除行数:deleteRowsAtIndexPaths:withRowAnimation:
注意:不管是局部刷新还是整体刷新,原理都是:UITableView重新向数据源(dataSource)和代理(delegate)发送相应的消息,最终将得到的数据展示出来
当定义数据源方法返回每一行显示的数据的时候,需要使用缓存池技术
static NSString *ID = @"C1";
//1.从缓存池中取出可循环利用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
//2.如果缓存池中没有可循环利用的cell,需要重新创建
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
但数据存储在plist文件中,生成字典使用
同时使用数据模型,这样的话在模型中提供两个方法对对象进行初始化
NSString *path = [[NSBundle mainBundle] pathForResource:@"shops" ofType:@"plist"];
NSArray *tempShops = [NSArray arrayWithContentsOfFile:path];
_shops = [NSMutableArray array];
for(NSDictionary *dict in tempShops)
{
shop *s = [[shop alloc] initWithDict:dict];
[_shops addObject:s];
}
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *desc;
@property (nonatomic, copy) NSString *icon;
- (id)initWithDict:(NSDictionary *)dict;
+ (id)shopWithDict:(NSDictionary *)dict;
- (id)initWithDict:(NSDictionary *)dict
{
黄色部分是固定的写法
if(self = [super init]){
self.name = dict[@"name"];
self.desc = dict[@"desc"];
self.icon = dict[@"icon"];
}
return self;
}
+ (id)shopWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
模拟淘宝的商品列表,可以选中多行,进行删除
#import "ViewController.h"
#import "shop.h"
@interface ViewController () <UITableViewDataSource,UITableViewDelegate>
{
NSMutableArray *_shops;
NSMutableArray *_deleteShops;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"shops" ofType:@"plist"];
NSArray *tempShops = [NSArray arrayWithContentsOfFile:path];
_shops = [NSMutableArray array];
for(NSDictionary *dict in tempShops)
{
shop *s = [[shop alloc] initWithDict:dict];
[_shops addObject:s];
}
_deleteShops = [NSMutableArray array];
}
#pragma mark - 删除
- (void)remove
{
在进行删除的时候:要么直接将需要删除的数据从数据模型中删除,然后整体刷新
要么将需要删除的行号存储在数组中,然后使用局部刷新
//获得索要删除数据的行号
NSMutableArray *deletePaths = [NSMutableArray array];
for (shop *s in _deleteShops) {
NSInteger row = [_shops indexOfObject:s];
NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0];
[deletePaths addObject:path];
}
//删除数据模型
[_shops removeObjectsInArray:_deleteShops];
//清空准备删除的数组
[_deleteShops removeAllObjects];
//刷新表格数据
//[self.tableView reloadData];
//局部的删除刷新,不需要整个tableView都刷新
[self.tableView deleteRowsAtIndexPaths:deletePaths withRowAnimation:UITableViewRowAnimationTop];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(_deleteShops.count == 0){
_removeBtn.enabled = NO;
_titelLabel.text = @"淘宝";
}else{
_titelLabel.text = [NSString stringWithFormat:@"淘宝(%lu)",_deleteShops.count];
_removeBtn.enabled = YES;
}
return _shops.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
shop *s = _shops[indexPath.row];
cell.textLabel.text = s.name;
cell.detailTextLabel.text = s.desc;
cell.imageView.image = [UIImage imageNamed:s.icon];
//判断勾选状态
if([_deleteShops containsObject:s]){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 70;
}
#pragma mark 监听cell的点击
#pragma mark 选中了某一行就会调用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//每个cell都有选中状态(selected)
//是个BOOL类型的值
//1.取消选中这样一行(去掉默认的蓝色背景)
[tableView deselectRowAtIndexPath:indexPath animated:YES];
shop *s = _shops[indexPath.row];
if([_deleteShops containsObject:s]){
[_deleteShops removeObject:s];
}else{
[_deleteShops addObject:s];
}
//2.刷新表格
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
}
#pragma mark 取消选中了某一行就会调用
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
@end