2013-03-18 135 views
12

我想在我的單元格上添加一個自定義按鈕,與滑動刪除功能做同樣的事情。所以當點擊我的自定義按鈕時,這個會隱藏起來讓官方出現紅色的「刪除」按鈕。以編程方式觸發UITableViewCell「刪除」按鈕

所以我做了這樣的事情:

/// Controller.m 
/// 
/// @brief Delete icon button pressed. Trigger display of Delete full button 
/// 
- (IBAction)deleteDrug:(id)sender event:(id)event { 
    NSIndexPath *indexPath = [self indexPathForButton:sender event:event]; 
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 
    [cell setEditing:YES animated:YES]; 
} 

/// CustomCell.m 
- (void)setEditing:(BOOL)editing animated:(BOOL)animated { 
    [super setEditing:editing animated:animated]; 

    // hide/show "modify" button when entering in edit mode 
    switch (editing) { 
     case YES: 
      self.deleteButton.hidden = YES; 
      break; 
     case NO: 
      self.deleteButton.hidden = NO; 
      break; 
     default: 
      break; 
    } 
} 

在這一刻,當你點擊它們,但官方的紅色「刪除」按鈕並未顯示在我的自定義按鈕越來越隱藏。

做一個人知道如何處理這個?

回答

5

我相信刪除按鈕更多地由tableView處理。因此,而不是設置你的單元格編輯,你可能需要讓tableView知道它應該是編輯。

- (IBAction)deleteDrug:(id)sender event:(id)event { 
    selectedButtonIndex = [self indexPathForButton:sender event:event]; 

    [tableView setEditing:YES animated:YES]; 
} 

所以你可能需要做一些事情,比如設置tableView來編輯。然後在你的tableview的數據源中,你可以實現這個方法,其中selectedButton是適當單元格的索引路徑。

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

    return NO; 
} 

您可能需要爲您的數據源實現此方法。

- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return UITableViewCellEditingStyleDelete; 
} 
+6

'[tableView setEditing:YES animated:YES];'會在單元格左邊放置一個紅色圖標(允許您調出右邊的紅色「刪除」按鈕)。我不想要它。我真的希望我的自定義按鈕直接顯示右邊的紅色「刪除」按鈕。 – Yaman 2013-03-18 15:57:13

+0

我想這是真的。它可能是因爲你試圖覆蓋預期的行爲,你可能需要去全距離並處理刪除按鈕。 – 2013-03-18 15:58:11

+0

葉我害怕這將是唯一的方式... Thx爲你的幫助瑞安。 – Yaman 2013-03-18 16:00:59

-3

請執行此操作以在UITableViewcell的右側使用您的刪除按鈕。

- (void)tableView:(UITableView *)tableView 
     commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
     forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [tableView beginUpdates]; 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] 
        withRowAnimation:UITableViewRowAnimationTop]; 
     [self.**yourarray** removeObjectAtIndex:indexPath.row]; 
     [tableView endUpdates]; 
     [tableView reloadData]; 
    } 
} 
+0

不適當的答覆。 – 2015-09-05 08:23:07

+0

不回答問題。 – Camsoft 2016-11-10 09:14:37

1

我沒有找到任何適當的解決方案來解決這個問題,所以我做了我自己的。我做了假刪除按鈕,它的工作原理非常像原來的那個。我在這裏顯示棘手的刪除按鈕部分。

如果你想知道如何更換默認減去(刪除)編輯控件,有關於它的好文章,我建議:http://vinsol.com/blog/2015/01/06/custom-edit-control-for-uitableviewcell/

所以,假設你發文章第一部分,你想看到按下自定義編輯控制按鈕時刪除按鈕。該代碼是雨燕,但您可以使用相同的步驟,使這個在Objective-C

func deleteButtonShow(){ 

    let deleteButton = UIButton(frame: CGRect(x: self.frame.width, y: 0, width: 80, height: self.frame.height)) 
    deleteButton.backgroundColor = UIColor.redColor() // ! note: original delete have another color 
    deleteButton.setTitle("Delete", forState: UIControlState.Normal) 
    deleteButton.setTitleColor(UIColor.whiteColor(), forState: .Normal) 
    deleteButton.addTarget(self, action: "actionForDelete", forControlEvents: UIControlEvents.TouchUpInside) 
    // action for your button will call delegate that will do the delete work. in most cases 
    self.contentView.addSubview(deleteButton) 

    var movedSubviews = self.subviews   
    var i = 0 
    for subview in movedSubviews { 
     // NOTE: I added tag = 1 in the storyboard to Content View of my cell 
     if subview.tag == 1 { 
      movedSubviews.removeAtIndex(i) // we don't need to move it. 
      break 
     } 
     ++i 
    } 
    movedSubviews.extend(self.contentView.subviews)// add subviews from content view 

    UIView.animateWithDuration(0.3, animations: {() -> Void in 

     for movedView in movedSubviews as! [UIView] { 

      movedView.frame = CGRect(origin: CGPoint(x: movedView.frame.origin.x - 80, y: movedView.frame.origin.y), size: movedView.frame.size) 
     //animate the things  
     } 

    }) 


} 

我們不希望因爲我發現移動內容的觀點,即如果我們的按鈕是不是在內容上面認爲它是不可觸摸的,我有一些想法爲什麼,但不知道。如果你知道爲什麼,請在評論中添加此信息,我很想知道它。

我還想指出,如果您使用autolayout作爲我,您需要更改動畫約束,而不是幀,所以您可能不需要extend部分。

相關問題