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效果及代碼覺得很不錯

謝謝。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,242評論 25 708
  • 遞歸算法要求 每次調用在規模上都有所縮小; 相鄰兩次重復之間有緊密的聯系,前一次要為后一次做準備(通常前一次的輸出...
    任教主來也閱讀 223評論 0 1
  • 昨晚聽了一節有關電信詐騙的微課,老師用自己切身經歷告訴我們如今信息安全狀況令人擔憂,人之所以上當受騙,更多是對官方...
    終身學習的細嗅薔薇閱讀 274評論 0 2
  • Hey,28 想和未來的自己打個招呼,今年相處得挺一般,而且我覺得咱們還不是很熟,客氣點沒錯的啦??(?ˉ???ˉ...
    清和晨曦閱讀 248評論 0 0
  • 文 | 駝玲 1. 昨晚,老友發給我美國新總統女兒伊萬卡·特朗普的演講視頻,并擱了“被刷屏了”四個字后閃人,也沒有...
    玲歌閱讀 838評論 2 2