When selecting multiple options, it is not selected by default. Re-select the option and upload the selected data to the background. However, in the following case, the added raw material elements will be displayed when selecting items by default.

Here I am using the MVC design pattern to do processing

  1. Add a Boolean value selected to Model
  2. When assigning a value to a View, you need to check whether the image is selected or not
  3. The current Controller creates an array of all elements and an array of selected elements and presents data
  4. Select the current row in the current Controller
  5. Click Ok to upload background operation logic

If the idea of multiple selection is similar to here, you can directly look at the third point: echo the problem before delivering the list.

Select * from Model; select * from Model; select * from Model; select * from Model

// id @property (nonatomic, copy) NSString *id; @property (nonatomic, copy) NSString *materialName; @property (nonatomic, assign) NSInteger isCheck; // Check @property (nonatomic, assign, getter=isSelected) BOOL selected;Copy the code

When assigning to a View, you need to check whether “Selected” is selected

// Model assignment - (void)setModel:(XHHMaterialModel *)model {
    
    _model = model;
    
    self.stock.text = model.materialName;
    
    if (model.selected == YES) {
        self.selectImg.image = [UIImage imageNamed:@"xz"];
    } else{
        self.selectImg.image = [UIImage imageNamed:@"wx"]; }}Copy the code

Create an array of all elements in the current Controller and an array of selected elements and display data

@property (nonatomic, strong) NSMutableArray *list; @property (nonatomic, strong) NSMutableArray *selectedEditList;Copy the code

- (NSMutableArray *)list {
    if(! _list) { _list = [NSMutableArray array]; }return _list;
}

- (NSMutableArray *)selectedEditList {
    if(! _selectedEditList) { _selectedEditList = [NSMutableArray array]; }return _selectedEditList;
}
Copy the code

The network gets the list data, and determines whether to check it according to the background definition of isCheck field. Here, we inform the background that if the value is 1, it will be checked, otherwise it will not be checked.

- (void)loadListNetData {// Remove all data [weakself. list removeAllObjects]; NSArray *currentPageArray = [XHHMaterialModel loadMaterialInfoFromJson:json[@"data"]]. [weakSelf.list addObjectsFromArray:currentPageArray]; // Iterate over the previously checked values in the display modelfor (XHHMaterialModel *model in currentPageArray) {
        if(model.ischeck == 1) {// 1 inserts this data into the multi-select array model.selected = YES; [self.selectedEditList addObject:model]; }else{ model.selected = NO; [self.selectedEditList removeObject:model]; }}}Copy the code

Select the current line in the current Controller

// Select the current row - (void)tableView (UITableView *)tableView didSelectRowAtIndexPath (NSIndexPath *)indexPath {XHHMaterialModel *model = self.list[indexPath.row]; // Select Model.selected =! model.selected; [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];if (model.selected) {
        [self.selectedEditList addObject:model];
    } else{ [self.selectedEditList removeObject:model]; } // Exit the editing state (close the keyboard) [self.view endEditing:YES]; }Copy the code

## 5. Click ok to upload background operation logic which we and background definition is to pass ID string

/ confirmation * * * * / - (void) bottomBtnPress {/ / send to increase raw material confirmed [self sendMaterialIdStrDataArray: self. SelectedEditList]; } / * * * send confirmation * / increase the raw material - (void) sendMaterialIdStrDataArray: (NSArray *) array {NSArray * idArray = [array valueForKeyPath: @"id"];
    NSString *materialIdStr = [idArray componentsJoinedByString:@","];
    self.materialIdStr = materialIdStr;
    
  if(self. MaterialIdStr. Length = = 0) {/ / this filter out don't send request [MBProgressHUD showError: @"Please choose your ingredients first."];
        return;
 } else if(self. SelectedEditList. Count > 5) {/ / multiple limit [MBProgressHUD showError: @"Choose up to 5 ingredients"];
        return; NSMutableDictionary *params = [NSMutableDictionary dictionary]; params[@"userId"] = [NSString stringWithString:[XHHAccountTool account].userid];
    params[@"materialIdStr"] = self.materialIdStr;
    
    [XHHHttpTool post:XHH_insetFormulaAdd_url params:params success:^(id json) {
        XHHLog(@"Request for additional raw material successful -%@", json);
        
        if ([json[@"success"] isEqual:@(YES)]) {
            
            [MBProgressHUD showSuccess:@"Success in adding raw materials"]; Dispatch_after (dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.7 * NSEC_PER_SEC)), dispatch_get_main_queue(), dispatch_get_main_queue(), ^ {/ / delay 0.7 s back [self. The navigationController popViewControllerAnimated: YES]; }); }else {
            [MBProgressHUD showError:[NSString stringWithFormat:@"% @", json[@"errorMessage"]]];
        }
        
    } failure:^(NSError *error) {
        [MBProgressHUD showError:@"No network. Try again later."];
        XHHLog(@"Request for additional raw material failed -%@", error);
    }];
}
Copy the code

PS: just a Jane Jane letter me :” pro can limit the number of tick? How to do that!” I will add the small ideas here, assuming a multiple-choice limit 5, judge the multiple-choice array number and then upload the background when it is ok to limit

 if(self. SelectedEditList. Count > 5) {/ / multiple limit [MBProgressHUD showError: @"Choose up to 5 ingredients"];
        return;
    }
    
Copy the code

OK, if you are interested, you can test it at 🙂