//
// TableViewController.m
#import "TableViewController.h"
#import "tgModel.h"
#import "tgCell.h"
@interface TableViewController ()
/** 所有团购数据 */
@property(nonatomic,strong) NSMutableArray *tgs;
- (IBAction)clickedDeleteButton:(UIBarButtonItem *)sender;
- (IBAction)clickedEditButton:(UIBarButtonItem *)sender;
@end
@implementation TableViewController
- (NSMutableArray *)tgs
{
if (_tgs == nil) {
// 加载plist文件中的字典数组
NSString *path = [[NSBundle mainBundle] pathForResource:@"tgs.plist" ofType:nil];
NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
// 字典数组 -> 模型数组
NSMutableArray *arrayM = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
tgModel *model = [tgModel tgWithDict:dict];
[arrayM addObject:model];
}
_tgs = arrayM;
}
return _tgs;
}
- (void)viewDidLoad {
[super viewDidLoad];
//允许在编辑模式进行多选操作
self.tableView.allowsMultipleSelectionDuringEditing = YES;
self.navigationItem.rightBarButtonItem.title = nil;
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.tgs.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 创建cell的过程封装到cell类中
tgCell *cell = [tgCell cellWithTableView:tableView];
//传递模型数据
cell.model = self.tgs[indexPath.row];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
- (IBAction)clickedDeleteButton:(UIBarButtonItem *)sender
{
// 获取所有被选中的行
NSArray *indexPaths = [self.tableView indexPathsForSelectedRows];
//遍历所有的行号
NSMutableArray *deleteds = [NSMutableArray array];
for (NSIndexPath *path in indexPaths) {
[deleteds addObject:self.tgs[path.row]];
}
//删除模型数据
[self.tgs removeObjectsInArray:deleteds];
//刷新表格
// [self.tableView reloadData];
//用下面这个方法有动画效果
[self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:YES];
}
/**
* 点击了导航栏左边按钮
*/
- (IBAction)clickedEditButton:(UIBarButtonItem *)sender
{
//切换编辑模式
if (self.tableView.isEditing) {
[self.tableView setEditing:NO animated:YES];
//更改leftBarButtonItem上的文字
self.navigationItem.leftBarButtonItem.title = @"编辑";
self.navigationItem.rightBarButtonItem.title = nil;
}else{
[self.tableView setEditing:YES animated:YES];
//更改leftBarButtonItem上的文字
self.navigationItem.leftBarButtonItem.title = @"取消";
self.navigationItem.rightBarButtonItem.title = @"删除";
}
}
@end
UITableView上的批量操作(系统方法)
最新推荐文章于 2021-10-27 16:00:29 发布