2015-10-20 3 views
8

Я ищу пару статей, но не нашел то, что искал. В принципе, я хочу показать кнопку удаления в каждой строке, но я не хочу использовать свойство UITableView.editing. Потому что это выглядит так;UITableViewCell, кнопка удаления удачного стиля без салфетки

enter image description here

Там будет кнопка "Редактировать". Когда пользователь нажимает на нее, кнопка удаления будет выглядеть как стиль салфетки.

Есть ли шанс показать кнопки удаления, подобные этому;

enter image description here

Может быть, есть какой-то способ справиться с ней. В противном случае я собираюсь создать для этого пользовательский вид.

Спасибо за ваш совет.

- (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

сделать и хотите удалить кнопку как IMAGE2 в Tableview? –

+0

@bhavinramani yes Я хочу удалить кнопки будет выглядеть как image2. Появится кнопка «Изменить», и когда вы нажмете на нее, все строки должны выглядеть как image2. –

+0

Пользовательская ячейка tableview является единственным способом для вас, так как кнопка удаления в конце ячейки не будет видна для всей ячейки в виде таблицы сразу (как вы разработали на снимке экрана) – viral

ответ

2
  • Добавить булево свойство: @property BOOL didPressEdit;
  • Добавить UIButton в UITableViewCell
  • При нажатии редактирования, didPressEdit становится TRUE и UITableView перезагружается, такие, что cell.deleteButton.hidden = !didPressEdit; что делает все Удалить кнопки, доступные
  • При нажатии удаление, удаление объекта из массива данных, перезагрузка tableview

Надеюсь, что это поможет

0

Вы можете использовать метод делегата UITableView, чтобы запросить эти действия. Внесите этот метод следующим образом:

- (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]; 
} 

Вы можете, конечно, возвратить несколько действий и настроить текст и цвет фона.

Реализация этого метода также требуется, чтобы сделать строку редактирования:

- (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. 
     } 
    } 

Вам нужно вызвать метод editActionsForRowAtIndexPath на вашем редактировать кнопки.

-(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

Я не уверен, что вы поняли вопрос. OP имеет особую потребность в том, чтобы сделать кнопку удаления доступной без салфетки и для всех строк в tableview при нажатии кнопки редактирования. Я не вижу ничего из достигнутого с предоставленным вами ответом. – 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 { 

} 
Смежные вопросы