2015-10-20 16 views
8

Tôi tìm kiếm vài bài viết nhưng tôi không tìm thấy những gì tôi đang tìm kiếm. Về cơ bản, tôi muốn hiển thị nút xóa trên mỗi hàng nhưng tôi không muốn sử dụng thuộc tính UITableView.editing. Bởi vì nó trông như thế này;UITableViewCell, hiển thị nút xóa kiểu swipe mà không cần vuốt

enter image description here

Sẽ có nút "Chỉnh sửa". Khi người dùng nhấp vào nó, nút xóa sẽ trông giống như kiểu swipe.

Có cơ hội hiển thị các nút xóa như thế này không;

enter image description here

Có thể có một cách nào đó để đối phó với nó. Nếu không, tôi sẽ tạo chế độ xem tùy chỉnh cho việc này.

Cảm ơn lời khuyên của bạn.

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Return YES if you want the specified item to be editable. 
    return YES; 
} 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 

     //Do something... 
    } 
} 
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView 
      editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { 

    return UITableViewCellEditingStyleDelete; 

} 
+0

làm u muốn nút xóa như image2 trong tableview? –

+0

@bhavinramani yes Tôi muốn xóa các nút sẽ trông giống như image2. Sẽ có nút "Chỉnh sửa" và khi bạn nhấp vào tất cả các hàng sẽ trông giống như image2. –

+0

Custom tableview cell là cách duy nhất để đi cho bạn, vì nút xóa ở cuối ô sẽ không hiển thị cho tất cả ô trong tableview cùng một lúc (như bạn đã xây dựng trong ảnh chụp màn hình) – viral

Trả lời

2
  • Thêm một tài sản boolean: @property BOOL didPressEdit;
  • Thêm UIButton-UITableViewCell
  • Khi nhấn chỉnh sửa, didPressEdit trở thành TRUEUITableView load lại, như vậy mà cell.deleteButton.hidden = !didPressEdit; mà làm cho tất cả Xóa nút có sẵn
  • Khi nhấn xóa, xóa đối tượng khỏi mảng nguồn dữ liệu, tải lại tableview

Hy vọng điều này sẽ giúp

0

Bạn có thể sử dụng phương thức ủy quyền UITableView để yêu cầu những hành động đó. Thực hiện phương pháp này như sau:

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewRowAction *modifyAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { 
    // Respond to the action. 
    }]; 
    modifyAction.backgroundColor = [UIColor blueColor]; 
    return @[modifyAction]; 
} 

Bạn có thể trả về nhiều hành động và tùy chỉnh màu văn bản và màu nền.

Thực hiện phương pháp này cũng được yêu cầu để làm cho hàng có thể chỉnh sửa:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
     if (editingStyle == UITableViewCellEditingStyleDelete) { 
      [self.objects removeObjectAtIndex:indexPath.row]; 
      [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     } else if (editingStyle == UITableViewCellEditingStyleInsert) { 
      // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. 
     } 
    } 

Bạn cần phải gọi phương thức editActionsForRowAtIndexPath vào nút chỉnh sửa của bạn nhấp chuột.

-(void)buttonTouched:(id)sender{ 

     UIButton *btn = (UIButton *)sender; 
     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:btn.tag inSection:0]; 
     [self tableView:self.tableView editActionsForRowAtIndexPath:indexPath]; 
    } 

    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
     // Return NO if you do not want the specified item to be editable. 
     return YES; 
    } 
+0

Tôi không chắc chắn bạn đã hiểu câu hỏi. OP có nhu cầu cụ thể để làm cho nút xóa có sẵn mà không cần vuốt và cho tất cả các hàng trong tableview trên tap của nút chỉnh sửa. Tôi không thấy bất kỳ điều nào đạt được với câu trả lời bạn cung cấp. – viral

2
-(NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewRowAction *moreAction2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Edit" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){ 
     [self.heartCartTabel setEditing:NO]; 
     [self editButtonClicked:indexPath.row]; 
    }]; 
    moreAction2.backgroundColor = [UIColor blueColor]; 

    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){ 

     [self tableView:self.heartCartTabel commitEditingStyle: UITableViewCellEditingStyleDelete forRowAtIndexPath:indexPath]; 
     //  [self.heartCartTabel deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
    }]; 

    return @[deleteAction, moreAction2]; 
} 

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

- (IBAction)editButtonClicked:(int)indexNumber { 

} 
Các vấn đề liên quan