标签:des style io color ar 使用 for sp strong
给局部变量加上关键字:static
static NSString *ID = @"C1";
//1.从缓存池中取出可循环利用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
//2.如果缓存池中没有可循环利用的cell,需要重新创建
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
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
标签:des style io color ar 使用 for sp strong
原文地址:http://www.cnblogs.com/yaofch107/p/4055451.html