iOS 中利用UITableview的編輯功能實現多選和單選

單選功能

首先我們

    _tableView.allowsMultipleSelectionDuringEditing = YES;
    [_tableView setEditing:YES animated:YES];

然后在 didSelectRowAtIndexPath 選中數據 數據添加到數組中

        NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:_index inSection:0];
        UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:lastIndex];
        lastCell.selected = YES;
        _index = indexPath.row;
        //afterDelay為延遲多少刪除上次的選中效果
        [_tableView performSelector:@selector(deselectRowAtIndexPath:animated:) withObject:lastIndex afterDelay:.0];

在 didDeselectRowAtIndexPath 從數組中移除數據

            NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:_index inSection:0];
            UITableViewCell *lastCell = [tableView cellForRowAtIndexPath:lastIndex];
            lastCell.selected = NO;
            _index = -1;
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}

多選

多選相對簡單點 直接使用就行

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

推薦閱讀更多精彩內容