1.gif最近由于换工作,好长时间没发表文章了,趁今晚闲来,发表一下,购物车处理相关的Demo
单选、全选处理
//全选
- (IBAction)clikeMainSleocted:(UIButton *)sender {
allprice = 0;
__weak typeof(self) weakSelf = self;
[self.data enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
WineModel *mod = obj;
mod.seleoctedShop = !sender.selected;
if(mod.seleoctedShop == NO){
self.moneyMain.text = @"0";
}else{
int total = mod.money.intValue * mod.shopCount;
allprice = allprice + total;
weakSelf.moneyMain.text = [NSString stringWithFormat:@"%d",allprice];
}
}];
[self.table reloadData];
sender.selected = ! sender.selected;
}
//选中
- (void)TableViewCellWithClikeSeleocCell:(TableViewCell *)cell{
if(cell.mod.seleoctedShop){
int total = self.moneyMain.text.intValue + cell.mod.money.intValue * cell.mod.shopCount;
self.moneyMain.text = [NSString stringWithFormat:@"%d",total];
}else{
int total = self.moneyMain.text.intValue - cell.mod.money.intValue * cell.mod.shopCount;
self.moneyMain.text = [NSString stringWithFormat:@"%d",total];
}
if(self.shopCar.count == self.data.count){
self.seleocMain.selected = YES;
}else{
self.seleocMain.selected = NO;
}
}
加、减处理
//加
- (void)TableViewCellWithClikeAddCell:(TableViewCell *)cell{
int main = self.moneyMain.text.intValue + cell.mod.money.intValue;
self.moneyMain.text = [NSString stringWithFormat:@"%d",main];
if(![self.shopCar containsObject:cell.mod]){
[self.shopCar addObject:cell.mod];
}
}
//减
- (void)TableViewCellWithClikeDeleocCell:(TableViewCell *)cell{
int main = self.moneyMain.text.intValue - cell.mod.money.intValue;
self.moneyMain.text = [NSString stringWithFormat:@"%d",main];
if(cell.mod.shopCount == 0){
[self.shopCar removeObject:cell.mod];
}
}
网友评论