最近由于換工作,好長時(shí)間沒發(fā)表文章了,趁今晚閑來,發(fā)表一下,購物車處理相關(guān)的Demo

1.gif
單選、全選處理
//全選
- (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];
}
}