IOS随笔-UITableView
一、常见数据源方法
#pragma mark - 让控制器成为数据源,必须遵守 <UITableViewDataSource>协议
1. 一共有多少组(如果不写此方法,默认为一组)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
2. 每一组显示多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
3.每一行显示怎样的cell(内容)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
二、代理方法
#pragma mark - 设置代理,必须遵守 <UITableViewDelegate>协议
1. 这只每一行cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
2. 每当选中cell的时候,就会被调用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
3. 取消选中,被调用
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
三、例子
#import "ViewController.h"
#import "Shop.h"
@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
{
NSMutableArray *_shop;
NSMutableArray *_deleteShops;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 加载plist文件
NSString *path = [[NSBundle mainBundle] pathForResource:@"shops.plist" ofType:nil];
NSArray *array = [NSArray arrayWithContentsOfFile:path];
// 从数组中取出字典,并把字典传 给shop, 将字典转成模型对象
_shop = [NSMutableArray array];
for (NSDictionary *dict in array) {
Shop *shop = [Shop shopWithDict:dict];
[_shop addObject:shop];
}
// 创建数组,将选中的行放入该数组中
_deleteShops = [NSMutableArray array];
}
#pragma mark 全选/取消全选
- (void)selectAllOrNone
{
if (_deleteShops.count == _shop.count) { // 取消全部选中
// 1. 清空 _deleteShops
[_deleteShops removeAllObjects];
} else { // 全中全部
// 1. 清空 _deleteShops
[_deleteShops removeAllObjects];
// 2. 添加 _shop 数组中得全部 模型数据到 _deleteShops 数组中
[_deleteShops addObjectsFromArray:_shop];
}
// 整体刷新
[_tableView reloadData];
}
#pragma mark 删除选中的cell
- (void)deledeSelectShop
{
// /* 整体刷新*/
// // 1. 将_deleteShops数组中的模型对象冲_shop数组中移除
// [_shop removeObjectsInArray:_deleteShops];
// // 2. 清空 _deleteShops数组
// [_deleteShops removeAllObjects];
// // 3. 重新加载数据
// [_tableView reloadData];
//
// 1. 获取要删除的行号
NSMutableArray *deleteIndex = [NSMutableArray array];
for (Shop *shop in _deleteShops) {
int row = [_shop indexOfObject:shop];
NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0];
[deleteIndex addObject:path];
}
// 2. 将 _deleteShops数组中得数据 从 _shop 数据delete
[_shop removeObjectsInArray:_deleteShops];
// 3. 清空_deleteShops数组
[_deleteShops removeAllObjects];
// 4.局部刷新
[_tableView deleteRowsAtIndexPaths:deleteIndex withRowAnimation:UITableViewRowAnimationLeft];
}
#pragma mark - 数据源方法
#pragma mark 一共有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 显示删除按钮和标题(每次刷新表格都会调用数据源方法)
if (_deleteShops.count) {
_trashItem.enabled = YES;
_labelTitle.text = [NSString stringWithFormat:@"淘宝(%d)", _deleteShops.count];
} else {
_trashItem.enabled = NO;
_labelTitle.text = @"淘宝";
}
// 如果 _shop 数组中的数据为空, 隐藏全选按钮
if (!_shop.count) _selectAll.enabled = NO;
return _shop.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:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
// 4. 取出indexpath 这行对应的shop模型对象
Shop *shop = _shop[indexPath.row];
// 5. 赋值
cell.textLabel.text = shop.name;
cell.detailTextLabel.text = shop.desc;
cell.imageView.image = [UIImage imageNamed:shop.icon];
// 6. 是否需要打钩
if ([_deleteShops containsObject:shop]) { // 需要打钩
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else { // 不需要打钩
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
#pragma mark - 代理方法
#pragma mark 每一行显示的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 70;
}
#pragma mark 选中一行就调用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// 取消默认的点击颜色
[tableView deselectRowAtIndexPath:indexPath animated:YES];
Shop *shop = _shop[indexPath.row];
// 如果之前已经选中, 从数组——delete中删除 改对象
if ([_deleteShops containsObject:shop]) { // 数组中已经存在改对象,删除
[_deleteShops removeObject:shop];
} else { // 数组中没有改对象,添加
[_deleteShops addObject:shop];
}
// 刷新
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
}
@end
浙公网安备 33010602011771号