編輯列表(刪除所選項)

效果圖以下圖所示json

注意:編輯頁面須要從新新建一個文件,不要想着重用原來的頁面數組

一、下面是編輯頁面須要實現的代碼 ide

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initWithUI];
    
    // Do any additional setup after loading the view from its nib.
}

- (void)initWithUI {
    self.navigationItem.title = QHLocalizedString(@"藏寶記錄", nil);
    
    self.editImageView.image = IMAGENAMED(@"capacity_noSele");//capacity_sele
    self.chooseReminder.text = QHLocalizedString(@"選擇所有", nil);
    [self.deleteButton setTitle:QHLocalizedString(@"刪除", nil) forState:UIControlStateNormal];
    
    _isDelete = NO;//是否刪除成功
    
    //初始化數組中的isSelect=NO;默認不點擊
    for (QHPiTreasureMyMineListModel *model in _resultArray) {
        model.isSelect = NO;
    }
    
    [self initWithTableView];
    
}

#pragma mark 初始化TableView
- (void)initWithTableView {
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    self.myTableView.tableFooterView = [UIView new];
    [self.myTableView registerNib:[UINib nibWithNibName:[QHBuriedTreasureEditTableViewCell identifier] bundle:nil] forCellReuseIdentifier:[QHBuriedTreasureEditTableViewCell identifier]];
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}

#pragma mark UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _resultArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    QHBuriedTreasureEditTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[QHBuriedTreasureEditTableViewCell identifier]];
    cell.model = _resultArray[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    QHPiTreasureMyMineListModel *model = _resultArray[indexPath.row];
    model.isSelect = !model.isSelect;
    [_resultArray replaceObjectAtIndex:indexPath.row withObject:model];
    
    NSIndexPath *curIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
    
    [self.myTableView reloadRowsAtIndexPaths:@[curIndexPath] withRowAnimation:UITableViewRowAnimationNone];
 
    
    NSArray *selectArray = [self screenSelectArray:YES];
    //若是將列表全部項都進行勾選了,選擇所有的按鈕也會變亮
    if (_resultArray.count == selectArray.count) {
        _editImageView.image = IMAGENAMED(@"capacity_sele");
        _allSelectBtn.selected = YES;
    }else{
         _editImageView.image = IMAGENAMED(@"capacity_noSele");
        _allSelectBtn.selected = NO;
    }
    
}

#pragma mark 根據isSelect進行謂詞篩選
- (NSArray *)screenSelectArray:(BOOL)isSelect{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"isSelect == %d",isSelect]];
    
    NSArray *selectArray = [_resultArray filteredArrayUsingPredicate:predicate];
    return selectArray;
}

#pragma mark 選擇所有
- (IBAction)didChooseAllClicked:(id)sender {
    UIButton *btn = sender;
    //點擊選擇所有按鈕->所有選擇或者所有取消選擇
    if (btn.selected) {
        [_resultArray setValue:@(NO) forKey:@"isSelect"];
    }else{
        [_resultArray setValue:@(YES) forKey:@"isSelect"];
    }
    
    [_myTableView reloadData];
    
    btn.selected = !btn.selected;
    if (btn.selected) {
         _editImageView.image = IMAGENAMED(@"capacity_sele");
    }else{
        _editImageView.image = IMAGENAMED(@"capacity_noSele");
    }
}

#pragma mark 刪除
- (IBAction)didDeleteClicked:(id)sender {
  
    [self showAlertWithTitle:[NSString stringWithFormat:@"%@?", QHLocalizedString(@"確認刪除", nil)] message:nil actions:@[QHLocalizedString(@"取消", nil),QHLocalizedString(@"確認", nil)] complete:^(NSInteger selIndex) {
        if (selIndex) {
            [self deleteList];
        }
    }];

}

#pragma mark 刪除列表接口
- (void)deleteList{
    NSArray *idsArray = [[self screenSelectArray:YES] valueForKey:@"treasureId"];
    if (idsArray.count == 0) {
        
        [MBProgressHUD showMessag:QHLocalizedString(@"請選擇須要刪除的記錄", nil) toView:self.view afterDelay:hiddenMBPTime];
        return;
    }
    
    [MBProgressHUD showHUDAddedTo:self.view withTitle:@"" animated:YES];
    QHPiBuriedDeleteBatchRequest *deleteApi = [[QHPiBuriedDeleteBatchRequest alloc]init];
 
    
    NSString *ids = [idsArray componentsJoinedByString:@","];
    deleteApi.ids = ids;
    [deleteApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        [self objectWithDeleteJson:request.responseJSONObject];
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    }];
}

- (void)objectWithDeleteJson:(id)json{
     [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    if (QH_VALIDATE_REQUEST(json)) {
         [MBProgressHUD showMessag:QHLocalizedString(@"刪除成功", nil) toView:self.view afterDelay:hiddenMBPTime];
        _isDelete = YES;
        [_resultArray removeObjectsInArray:[self screenSelectArray:YES]];
        [_myTableView reloadData];
        
    }else{
        [MBProgressHUD showMessag:json[@"msg"] toView:self.view afterDelay:hiddenMBPTime];
    }
}

#pragma mark 返回按鈕
- (void)leftClick:(UIButton *)btn{
    [self.navigationController popViewControllerAnimated:YES];
    if (_isDelete) {
        if (_successBlock) {
            _successBlock();
        }
    }
}

- (void)deleteSuccess:(void (^)())successBlock{
    _successBlock = successBlock;
}

二、未編輯頁面的跳轉code

#pragma mark 編輯
- (void)rightClick:(UIButton *)btn {
    [_screenArray removeAllObjects];
//由於是訂單,因此未支付的狀況下不能刪除,因此須要根據paystate==2進行謂詞篩選已經進行支付的訂單
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"paystate CONTAINS '2'"];
    
    [_screenArray addObjectsFromArray:[_dataArray filteredArrayUsingPredicate:predicate]];

    if (_screenArray.count>0) {
        QHPiBuriedTreasureEditViewController *editVC = [[QHPiBuriedTreasureEditViewController alloc] initWithNibName:@"QHPiBuriedTreasureEditViewController" bundle:nil];
        editVC.resultArray = [NSMutableArray arrayWithArray:_screenArray];
        __weak typeof(self) weakSelf = self;
        [editVC deleteSuccess:^{
            __strong typeof (self)strongSelf = weakSelf;
            [strongSelf updateRefrensh];
        }];
        [self.navigationController pushViewController:editVC animated:YES];
    }else {
        [MBProgressHUD showMessag:QHLocalizedString(@"沒有能夠編輯的記錄", nil) toView:self.view afterDelay:hiddenMBPTime];
    }
    
}

- (void)updateRefrensh {
    _pageIndex = 1;
    [self loadMyMineListRequest];
}

💪⛽️component

相關文章
相關標籤/搜索