ios表格刪除多選及全選

效果圖

Untitled3.gif

代碼



@property (strong, nonatomic) IBOutlet UIBarButtonItem *deleteBtn;

@property (strong, nonatomic) IBOutlet UIBarButtonItem *editBtn;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *allselect;



@property (nonatomic ,retain) NSMutableArray *dataArray;
@property (nonatomic, retain) NSMutableDictionary *deleteDic;


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationController.navigationBar.translucent = NO;
    [self startView];
}
- (void)startView {
    self.dataArray = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",nil];
    self.deleteDic = [[NSMutableDictionary alloc] init];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.editBtn.title = @"編輯";
    
    self.allselect.enabled = NO;
    self.allselect.title = @"";
    
}

#pragma mark -IBAction-

- (IBAction)editData:(id)sender {
    if ([_editBtn.title isEqualToString:@"編輯"]) {
        _editBtn.title = @"確定";
        [self.tableView setEditing:YES animated:YES];
        self.allselect.enabled = YES;
        self.allselect.title = @"全選";
    } else {
        _editBtn.title = @"編輯";
        [_deleteDic removeAllObjects];
        [self.tableView setEditing:NO animated:YES];
        self.allselect.enabled = NO;
        self.allselect.title = @"";
    }
}

- (IBAction)deleteAction:(id)sender {
    [_dataArray removeObjectsInArray:[_deleteDic allKeys]];
    
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithArray:[_deleteDic allValues]] withRowAnimation:UITableViewRowAnimationFade];
    [_deleteDic removeAllObjects];
}

- (IBAction)allselect:(id)sender {
    for (int row=0; row<_dataArray.count; row++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
        [_tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionTop];
        [_deleteDic setObject:indexPath forKey:[_dataArray objectAtIndex:indexPath.row]];
    }
    
}


#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [_dataArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
   
    
    // Configure the cell...
    cell.textLabel.text = [_dataArray objectAtIndex:indexPath.row];
    return cell;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}


#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"選中");
    if ([_editBtn.title isEqualToString:@"確定"]) {
        [_deleteDic setObject:indexPath forKey:[_dataArray objectAtIndex:indexPath.row]];
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([_editBtn.title isEqualToString:@"確定"]) {
        [_deleteDic removeObjectForKey:[_dataArray objectAtIndex:indexPath.row]];
    }
    

這是我之前下載的別人的demo效果及代碼覺得很不錯

謝謝。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,023評論 25 709
  • 遞歸算法要求 每次調(diào)用在規(guī)模上都有所縮小; 相鄰兩次重復(fù)之間有緊密的聯(lián)系,前一次要為后一次做準(zhǔn)備(通常前一次的輸出...
    任教主來也閱讀 259評論 0 1
  • Hey,28 想和未來的自己打個招呼,今年相處得挺一般,而且我覺得咱們還不是很熟,客氣點(diǎn)沒錯的啦??(?ˉ???ˉ...
    清和晨曦閱讀 262評論 0 0
  • 文 | 駝玲 1. 昨晚,老友發(fā)給我美國新總統(tǒng)女兒伊萬卡·特朗普的演講視頻,并擱了“被刷屏了”四個字后閃人,也沒有...
    玲歌閱讀 961評論 2 2
  • 在具體項(xiàng)目的選擇上,投資人會從以下十個維度來考察。 創(chuàng)業(yè)者也可以對照項(xiàng)目做個自我診斷,這樣可以避免在初期走彎路。 ...
    黑馬學(xué)吧閱讀 1,278評論 0 0

友情鏈接更多精彩內(nèi)容