第五章:数据更新
数据更新:添加,删除,更改
添加
- 需要直接对数据源操作,并且在操作完成之后需要对tableView进行刷新
// tableView里面如果要显示新的数据,只需要操作模型数据添加到数据源数组里
JXDeals * deal = [[JXDeals alloc] init];
[self.deals insertObject:deal atIndex:0];
// 提醒tableView模型数据发生了变化,请重新识别
// [self.tableView reloadData];
// 刷新某些特定行
[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];
1.对于上面的刷新方法,[self.tableView reloadData];是对整个数据源进行刷新;
2.[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];是对某一行数据进行刷新
3.当对某一行数据进行刷新的时候我们可以控制动画,并且效率更高;
4.当添加了几行的时候,我们动画方法也要同时添加几行。否则会出现[UITableView _endCellAnimationsWithContext:]错误
- 删除方法,同添加方法一样,对数据源进行操作
// 同样知识删除某一行
[self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationRight];
- 刷新方法,同上
[self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:3 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
数据刷新的原则
- 1.通过修改模型数据,来修改tableView的展示
- 先修改模型数据
- 在刷新cell方法
- 2.不要直接在cell上修改模型数据
删除数据
- 1.删除数据
// 进入编辑模式 会调用(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath方法,返回的就是右边显示的编辑模式.默认出现的删除按钮
[self.tableView setEditing:YES animated:YES];
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
/**
* 只要实现这个方法,左滑cell就会出现删除按钮
* 用户提交了删除(点击了删除按钮)/添加(点击了添加按钮)按钮的时候调用这个方法
* 也可以用户调用 [self.tableView setEditing:YES animated:YES]方法之后点击删除调用这个方法
*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) { // 如果是删除按钮
[self.deals removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
} else if (editingStyle == UITableViewCellEditingStyleInsert) { // 如果是添加按钮
}
}
批量操作
- 思路,现在cell模型中设置一个图片,将图片默认设置为隐藏
- 在模型中设置一个bool属性,用来标识是否勾选
- 在控制器中设置
数据模型
#import <Foundation/Foundation.h>
@interface JXDeals : NSObject
/** 团购图片 */
@property (nonatomic,strong) NSString * icon;
/** 团购标题 */
@property (nonatomic,strong) NSString * title;
/** 团购价格 */
@property (nonatomic,strong) NSString * price;
/** 团购数量 */
@property (nonatomic,strong) NSString * buyCount;
/** 团购标识 */
@property (nonatomic,assign,getter=isCheck) BOOL check;
+ (instancetype)dealsWithDict:(NSDictionary *)dict;
@end
```objc
//
// JXDealCell.m
// JX-自定义等高cell(storyboard)
//
// Created by 王加祥 on 16/4/18.
// Copyright © 2016年 Wangjiaxiang. All rights reserved.
//
#import "JXDealCell.h"
#import "JXDeals.h"
@interface JXDealCell ()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *buyCountLabel;
@property (weak, nonatomic) IBOutlet UIImageView *checkView;
@end
@implementation JXDealCell
- (void)setDeal:(JXDeals *)deal {
_deal = deal;
self.iconView.image = [UIImage imageNamed:deal.icon];
self.titleLabel.text = deal.title;
self.priceLabel.text = deal.price;
self.buyCountLabel.text = [NSString stringWithFormat:@"共%@团购",deal.buyCount];
self.checkView.hidden = !deal.isCheck;
}
+ (instancetype)cellWithTableView:(UITableView *)tableView {
static NSString * identifier = @"cell";
JXDealCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
// 第一种解决办法
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
}
return cell;
}
@end
控制器中
//
// JXTableViewController.m
// JX-自定义等高cell(storyboard)
//
// Created by 王加祥 on 16/4/18.
// Copyright © 2016年 Wangjiaxiang. All rights reserved.
//
#import "JXTableViewController.h"
#import "JXDeals.h"
#import "JXDealCell.h"
@interface JXTableViewController ()<UITableViewDelegate,UITableViewDataSource>
/** 团购数组 */
@property (nonatomic,strong) NSMutableArray * deals;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
@implementation JXTableViewController
- (IBAction)remove {
// 构建临时数组存放需要删除的数据
NSMutableArray * delete = [NSMutableArray array];
for (JXDeals *deal in self.deals) {
if (deal.isCheck) {
[delete addObject:deal];
}
}
// 删除数据
[self.deals removeObjectsInArray:delete];
// 刷新
[self.tableView reloadData];
~~ // 获取多行选中的cell
NSArray * array = [self.tableView indexPathsForSelectedRows];
NSMutableArray * delete = [NSMutableArray array];
for (NSIndexPath * patn in array) {
[delete addObject:self.deals[patn.row]];
}
[self.deals removeObjectsInArray:delete];
[self.tableView reloadData];~~
}
- (IBAction)mutableDelete {
[self.tableView setEditing:!self.tableView.isEditing animated:YES];
}
- (NSMutableArray *)deals {
if (_deals == nil) {
// 数据路劲
NSString * path = [[NSBundle mainBundle] pathForResource:@"tgs.plist" ofType:nil];
NSArray * array = [NSArray arrayWithContentsOfFile:path];
NSMutableArray * deals = [NSMutableArray array];
for (NSDictionary * dict in array) {
JXDeals * deal = [JXDeals dealsWithDict:dict];
[deals addObject:deal];
}
_deals = deals;
}
return _deals;
}
- (void)viewDidLoad {
[super viewDidLoad];
// 在编辑的时候允许多选
~~self.tableView.allowsMultipleSelectionDuringEditing = YES;~~
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.deals.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
JXDealCell * cell = [JXDealCell cellWithTableView:tableView];
JXDeals * deal = self.deals[indexPath.row];
cell.deal = deal;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
**JXDeals * deal = self.deals[indexPath.row];
deal.check = !deal.isCheck;**
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
}
@end
源码地址多选删除