關於tableView多選刪除的編輯設定
// 設定tableView是否可以刪除
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
// 設定刪除操作時候的標題
-(NSString*)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"刪除";
}
//問題2:某一行支援哪種編輯模式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
returnUITableViewCellEditingStyleDelete;//|UITableViewCellEditingStyleInsert;
}
tableView進入編輯狀態,左滑刪除-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle
NSLog(@"%ld",(long)editingStyle);
if (editingStyle==UITableViewCellEditingStyleDelete)
{
[SVProgressHUDshow];
if (self.DeleteCollectionBlock)
{
self.DeleteCollectionBlock();
}
NSString *codeString=[NSString
NSString *code=[NSString md5:codeString];
// MD5加密
NSString *urlString=[NSString stringWithFormat:、xxxxxxxxxxxxxxxxxxx];
NSMutableDictionary *params = [NSMutableDictionarydictionary];
params[@"user_id"] = UserID;
params[@"act"] = @"delete";
params[@"rec_id"] = [dataArray[indexPath.row] objectForKey:@"rec_id"];
[[UIApplicationsharedApplication]setNetworkActivityIndicatorVisible:NO];
NSLog(@"%@",urlString);
[WXNetworking WXBaseNetworkWithPOST:xxxxxx params:params success:^(id responseObject) {
// 刪除成功後移除資料來源並重新整理tableView
[dataArray removeObjectAtIndex:indexPath.row];
[tableView endUpdates];
[tableView reloadData];
} failure:^(NSError *error) {
}];
}
if (editingStyle == UITableViewCellEditingStyleInsert) {
NSIndexPath *newIP=[NSIndexPathindexPathForItem:dataArray.count-1inSection:0];
[tableView insertRowsAtIndexPaths:@[newIP]withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
因為cell的複用的關係,所以要對cell的選中按鈕做判斷,在從檢視中從新出現的時候要判斷之前是否再選中組裡面 同樣是遍歷陣列的方式來判斷。- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
for (UIView *view in cell.subviews) {
if ([view isKindOfClass:NSClassFromString(@"UITableViewCellEditControl")])
{
for (UIView *vVV in view.subviews) {
NSLog(@"%@",vVV);
[vVV removeFromSuperview];
}
WXButton* checkButton= [self checkBox:CGRectMakeE(0,0,47,110)];
NSLog(@"%ld",(long)indexPath.row);
NSInteger row=indexPath.row;
int ii=(int)row;
NSLog(@"%d",ii);
checkButton.hang=ii;
[checkButton addTarget:selfaction:@selector(checkButton:) forControlEvents:UIControlEventTouchUpInside];
for (NSDictionary *temDict1 in _selectArray)
{
if (dataArray[indexPath.row] == temDict1) {
checkButton.selected = YES;
}else{
}
}
[view addSubview:checkButton];
}
}
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
cell.preservesSuperviewLayoutMargins = NO;
}