在实现这个功能的时候,逻辑是这样的:
有两个数组,一个是所有条目的数组,一个是选中条目的数组
首先,在点按任何按钮的时候,将计算总额与页面的显示(按钮选中状态与总金额的显示)分开;
其次,点击“全选”按钮只管全选按钮的状态,不用管每个条目的状态。全选中时,将所有的条目添加到选中的数组中,再次点击取消全选,然后将选中数组清空。每个条目的状态显示放到刷新tableView里面,判断当前条目的内容有没有再选中的数组中,然后显示每个条目的状态;
然后,点击每个每个cell上的选中按钮的时候,给cell添加代理,点击时修改当前cell 的状态显示,在controller里面调用cell 的代理方法,将cell对应的内容和选中状态传过来,选中,则添加到选中数组中,取消,则从选中数组移除,同时判断选中数组的条目个数是否与所有条目数组的个数是否相同,从而修改全选按钮的状态显示
最后 ,就是总额的显示问题。在每次点击任何一个按钮的时候,都调用总额计算与显示的方法,在这个方法中,循环获取选中数组中的每个条目的价格,然后累加。
代码:
controller 中
@property (nonatomic, assign) BOOL selectAll;全选状态
#pragma HadExpireBillCellDelegate (cell 的代理实现)
- (void)selectChange:(BOOL)isSelect bill:(NSDictionary *)bill{
NSMutableSet *selectSet = [NSMutableSet setWithArray:_selectBillArr];
if (!isSelect) {
if ([selectSet containsObject:bill]) {
[_selectBillArr removeObject:bill];
}
}else{
if (![selectSet containsObject:bill]) {
[_selectBillArr addObject:bill];
}
}
//是否为全选
if (_selectBillArr.count == _billArr.count) {
self.selectAll = YES;
}else{
self.selectAll = NO;
}
[self getAmount];
}
#pragma actions
//全选(按钮点击方法)
- (IBAction)selectAllBtnAction:(UIButton *)sender {
self.selectAll = !_selectAll ? YES : NO;
[self.tableView reloadData];
}
//selectAll 的set 方法(每次赋值便会调用)
- (void)setSelectAll:(BOOL)selectAll{
_selectAll = selectAll;
if (_selectAll) {
[_selectAllBtn setImage:[UIImage imageNamed:@"billSelect"] forState:(UIControlStateNormal)];
[self.selectBillArr addObjectsFromArray:_billArr];
}else{
[_selectAllBtn setImage:[UIImage imageNamed:@"billNoSelect"] forState:(UIControlStateNormal)];
[_selectBillArr removeAllObjects];
}
[self getAmount];
}
//计算总金额
- (void)getAmount{
float amount = 0;
if (_selectBillArr.count == 0) {
_allMoneyL.text = @"0.00元";
}else{
for (NSDictionary *info in _selectBillArr) {
float price = [info[@"total"] floatValue];
amount += price;
}
_allMoneyL.text = [NSString stringWithFormat:@"%.2f元",amount];
}
}
/刷新cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
HadExpireBillCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HadExpireBillCell class])];
cell.infoDic = _billArr[indexPath.row];
NSMutableSet *selectSet = [NSMutableSet setWithArray:_selectBillArr];
if ([selectSet containsObject:_billArr[indexPath.row]]) {
cell.isSelected = YES;
}else{
cell.isSelected = NO;
}
cell.delegate = self;
return cell;
}
cell中
@protocol HadExpireBillCellDelegate
- (void)selectChange:(BOOL)isSelect bill:(NSDictionary *)bill;
@end
@interface HadExpireBillCell : UITableViewCell
@property (nonatomic, strong) NSDictionary *infoDic;
@property (nonatomic, assign) BOOL isSelected;
@property (nonatomic, assign) id<HadExpireBillCellDelegate> delegate;
@end
@implementation HadExpireBillCell
- (void)setInfoDic:(NSDictionary *)infoDic{
_infoDic = infoDic;
}
- (void)setIsSelected:(BOOL)isSelected{
_isSelected = isSelected;
if (isSelected) {
[_selectBtn setImage:[UIImage imageNamed:@"billSelect"] forState:(UIControlStateNormal)];
}else{
[_selectBtn setImage:[UIImage imageNamed:@"billNoSelect"] forState:(UIControlStateNormal)];
}
}
#pragma actions
- (IBAction)selectBtnAction:(UIButton *)sender {
_isSelected = !_isSelected ? YES : NO;
if (_isSelected) {
[_selectBtn setImage:[UIImage imageNamed:@"billSelect"] forState:(UIControlStateNormal)];
}else{
[_selectBtn setImage:[UIImage imageNamed:@"billNoSelect"] forState:(UIControlStateNormal)];
}
[self.delegate selectChange:_isSelected bill:_infoDic];
}
@end