Whether it is to do a shopping software, or other software, shopping cart or similar shopping cart pages are very common.
Common shopping carts can be divided into local and storage servers. Where there exists is little difference in the development of our client.
In the case of storage locally, we can use data base Do it if data structure Not complicated, you can also use NSUserDefaults to store an array, but remember not to pass in empty.
Let's take such a simple interface effect as an example:

There is a button on the left of each line to select, or to select all.
We can lay out the buttons on each line with a custom UIButton, and add an attribute to the button to record the line number.
To simplify the operation, we add listeners to the full selection button to distinguish the selected state from the default state, just like this.

Add such code to the viewdidload:
-
[_allChooseButton addObserver:self forKeyPath:@"buttonState" options:NSKeyValueObservingOptionNew context:@"_allChooseButtonStateChange"];
And implement its callback method:
-
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(voidvoid *)context {
-
if (context == @"_allChooseButtonStateChange") {
-
if (_allChooseButton.buttonState == ButtonStateNormal) {
-
[_allChooseButton setBackgroundImage:[UIImage imageNamed:@"goods_n"] forState:UIControlStateNormal];
-
}else {
-
[_allChooseButton setBackgroundImage:[UIImage imageNamed:@"goods_s"] forState:UIControlStateNormal];
-
}
-
}
-
}
Next, we need to complete the judgement and storage of the selected, unchecked, all-selected, and other states.
We can prepare a global variable array and initialize it to store the selected state of each of all the data.
-
_allCellStateArray = [[NSMutableArray alloc]init];
And we put the selected state of each object of our data into an array, or the state of each line, so that he is not selected by default:
-
for (int i = 0; i < _resultValue.count; i ++) {
-
NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
-
[dic setValue:@"0" forKey:[NSString stringWithFormat:@"%ld",(long)i]];
-
[_allCellStateArray addObject:dic];
-
}
The key in the dictionary is the current line number and value is the selected state. We use 1 to indicate that it is selected and 0 to indicate that it is not selected.
When we click on the full selection button, the picture of the button will change, and the selected state will change. First, add the code in the cell forrow method:
-
cell.chooseButton.indexNumber = indexPath.row;
-
if ([[[_allCellStateArray objectAtIndex:indexPath.row] objectForKey:[NSString stringWithFormat:@"%ld",(long)indexPath.row]] intValue]==0) {
-
-
cell.chooseButton.buttonState = ButtonStateNormal;
-
}else {
-
cell.chooseButton.buttonState = ButtonStateClicked;
-
}
-
if (cell.chooseButton.buttonState == ButtonStateNormal) {
-
-
[cell.chooseButton setBackgroundImage:[UIImage imageNamed:@"goods_n"] forState:UIControlStateNormal];
-
}else {
-
-
[cell.chooseButton setBackgroundImage:[UIImage imageNamed:@"goods_s"] forState:UIControlStateNormal];
-
}
-
[cell.chooseButton addTarget:self action:@selector(cellChooseClick:) forControlEvents:UIControlEventTouchUpInside];
The above two pieces of code can be written in an if statement.
When we click on the button, we change the status of the button and modify the array that holds our status.
-
#pragma mark - Selection
-
- (void)cellChooseClick:(EMButton *)sender {
-
if (sender.buttonState == ButtonStateNormal) {
-
-
NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
-
[dic setValue:@"1" forKey:[NSString stringWithFormat:@"%ld",(long)sender.indexNumber]];
-
[_allCellStateArray replaceObjectAtIndex:sender.indexNumber withObject:dic];
-
BOOL isAll = YES;
-
for (int i = 0; i < _allCellStateArray.count; i ++) {
-
int a = [[[_allCellStateArray objectAtIndex:i] objectForKey:[NSString stringWithFormat:@"%d",i]] intValue];
-
if (a == 0) {
-
isAll = 0;
-
}
-
}
-
if (isAll) {
-
-
-
_allChooseButton.buttonState = ButtonStateClicked;
-
}
-
-
}else {
-
-
-
-
_allChooseButton.buttonState = ButtonStateNormal;
-
-
NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
-
[dic setValue:@"0" forKey:[NSString stringWithFormat:@"%ld",(long)sender.indexNumber]];
-
[_allCellStateArray replaceObjectAtIndex:sender.indexNumber withObject:dic];
-
}
-
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:sender.indexNumber inSection:0];
-
[self.mainTable reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationMiddle];
-
}
The bool judgment in the middle is used to realize: when the user chooses all rows in the incomplete selection state, the fully selected button becomes the fully selected state; when the user clicks the fully selected button to enter the fully selected state, and then cancels one of the rows, the fully selected state is cancelled. As shown in the picture:

At this point, all the selection cancellation logic is complete, whether it is clicking the submit button to get the selected object, or displaying the quantity and price of the goods below. We can all retrieve objects from our data source by saving an array of data states for each row.
-
#pragma mark - submit button
-
- (void)submitMethod:(EMButton *)sender {
-
-
NSMutableArray *array = [[NSMutableArray alloc]init];
-
for (int i = 0; i < _allCellStateArray.count; i ++) {
-
if ([[[_allCellStateArray objectAtIndex:i] objectForKey:[NSString stringWithFormat:@"%d",i]] intValue]==1) {
-
[array addObject:[_resultValue objectAtIndex:i]];
-
}
-
}
-
NSLog(@"submit:%@ %ld",array,(long)array.count);
-
-
}
The method used to change the quantity of goods below:
-
- (void)changeInfoLabel {
-
NSMutableArray *array = [[NSMutableArray alloc]init];
-
for (int i = 0; i < _allCellStateArray.count; i ++) {
-
if ([[[_allCellStateArray objectAtIndex:i] objectForKey:[NSString stringWithFormat:@"%d",i]] intValue]==1) {
-
[array addObject:[_resultValue objectAtIndex:i]];
-
}
-
}
-
-
_countNumber = (int)array.count;
-
_money = 0;
-
for (int i = 0;i < array.count; i ++) {
-
_money += [[[array objectAtIndex:i] objectForKey:@"buyNum"] intValue];
-
}
-
_tabInfoLabel.text = [NSString stringWithFormat:@"common%d Commodity,common%d element",_countNumber,_money];
-
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:_tabInfoLabel.text];
-
UIColor * color1 = [UIColor colorWithRed:214.0/255.0 green:74.0/255.0 blue:115.0/255.0 alpha:1.0] ;
-
[str addAttribute:NSForegroundColorAttributeName value:color1 range:NSMakeRange(1,[NSString stringWithFormat:@"%d",_countNumber].length)];
-
[str addAttribute:NSForegroundColorAttributeName value:color1 range:NSMakeRange(1+[NSString stringWithFormat:@"%d",_countNumber].length+5,[NSString stringWithFormat:@"%d",_money].length)];
-
-
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica" size:16.0] range:NSMakeRange(1, 2)];
-
-
_tabInfoLabel.attributedText = str;
-
}
We can call this method in the method of the full selection button and the method of the radio button, so that we can change the quantity and price information below every time we click.
Our shopping cart is basically finished.
If you want to add or subtract the quantity of a commodity, or delete the function of a commodity, such as:


Then we need to add more judgments, such as the add and subtract button, which will not affect the array where we store the selected state of each line. This change is the data source. We need to change the data source that we stored locally in real time (also can store uniformly when we exit the shopping cart page, but if the user exits the program at this time, it will not be able to store the new changes. Data).
The deletion function requires us to modify the data file at the same time, also do not forget that we need to modify the status array at the same time. In this way, our shopping cart has two new functions.
If your shopping cart data is downloaded from the server and stored locally, but we can't modify the data file from time to time, here we'd better agree to modify when we leave this page, otherwise it will take a lot of traffic.
Finally, the core part is summarized: each data source is stored in a separate dictionary, key is the line number, value is the selected state, and these dictionaries are stored in an array.
Let's start by saying so much. I'm still a shallow person. Welcome to leave a message with better suggestions.