Có cách nào để ẩn nút “-” (Xóa) trong khi chỉnh sửa UITableView không


97

Trên ứng dụng iphone của tôi, tôi có UITableView ở chế độ chỉnh sửa, nơi người dùng chỉ được phép sắp xếp lại các hàng mà không có quyền xóa được cấp.

Vì vậy, có cách nào mà tôi có thể ẩn nút màu đỏ "-" khỏi TableView. Làm ơn cho tôi biết.

Cảm ơn

Câu trả lời:


258

Đây là giải pháp hoàn chỉnh của tôi, không có thụt lề (0left align) của ô!

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleNone; 
}

- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}


- (BOOL)tableView:(UITableView *)tableview canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

43

Swift 3 tương đương với câu trả lời được chấp nhận chỉ với các chức năng cần thiết:

func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
    return false
}

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
    return .none
}

4

Thao tác này sẽ ngừng thụt lề:

- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

3

Tôi gặp phải sự cố tương tự trong đó tôi muốn các hộp kiểm tùy chỉnh xuất hiện trong chế độ Chỉnh sửa nhưng không phải nút xóa '(-)'.

Câu trả lời của Stefan đã hướng tôi đi đúng hướng.

Tôi đã tạo một nút bật tắt và thêm nó dưới dạng một editAccessoryView vào Ô và kết nối nó với một phương thức.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    ....
    // Configure the cell...

    UIButton *checkBoxButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 40.0f, 32.0f)];
    [checkBoxButton setTitle:@"O" forState:UIControlStateNormal];
    [checkBoxButton setTitle:@"√" forState:UIControlStateSelected];
    [checkBoxButton addTarget:self action:@selector(checkBoxButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

    cell.editingAccessoryType = UITableViewCellAccessoryCheckmark;
    cell.editingAccessoryView = checkBoxButton;

    return cell;
}

- (void)checkBoxButtonPressed:(UIButton *)sender {
    sender.selected = !sender.selected;
}

Đã triển khai các phương thức ủy quyền này

- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleNone;
}

0

Khi bạn chỉ muốn ẩn dấu chấm (-) trong khi chỉnh sửa nhưng bạn có thể muốn giữ chức năng xóa cho những người dùng mà bạn triển khai nó như vậy trong UITableViewDelegatelớp tuân thủ giao thức của bạn

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.editing) return UITableViewCellEditingStyleNone;
    return UITableViewCellEditingStyleDelete;
}
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.