2013-10-09 30 views
4

聲明:我知道調整這種東西不是最佳實踐,因爲它可能會因爲蘋果決定改變其內部行爲而中斷。iOS 7:UITableViewController:更改/替換刪除按鈕

有一些解決方案有像https://stackoverflow.com/a/12511432/271150這似乎是工作以前的IOS版本,但不適用於iOS的7

當尋找到控制層次,我可以看到存在UITableViewCellScrollView內UITableViewCellDeleteConfirmationView。但通過查看layoutSubviews或willTransitionToState中的SubViews集合,只有我自己的視圖,UITableViewCellDeleteConfirmationView不會出現。

enter image description here

那麼,有沒有人想出如何修改默認的刪除按鈕/看法?

+0

可能重複:http://stackoverflow.com/questions/17254402/swipe-to-delete-and-the-more-button-像在郵件應用程序上的IOS 7 – Krumelur

+0

沒有。也沒有正確回答。 –

+0

對於「如何調整你不應該調整的UI層次結構」沒有正確的答案 - 它永遠是一個破解。第二和第三個答案看起來非常像一個有效的方法。 – Krumelur

回答

-1

不知道這是否適用於iOS 7,但看一看:

(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    if (editingStyle == UITableViewCellEditingStyleDelete) 

    { 

    // Your Code 

    } 
} 
+0

點擊按鈕時會調用此方法,所以它遲到了 - 但是謝謝;-)。 –

4

我知道你可能不希望覆蓋蘋果整個執行編輯表格視圖單元格,但我有一個類似的問題,我的應用程序。在我的情況下,我需要兩個按鈕(更多和丟失)來顯示,就像在用戶從左向右滑動時iOS7的新提醒應用中一樣。代替使用:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 

我創建的隱藏頂層下面的按鈕,並使用一個平移手勢識別器來移動頂層,顯示所述按鈕下方的自定義單元格。因此,我可以完全控制顯示哪些按鈕以及它們是什麼顏色(見下文)。

static CGFloat const kEditButtonWidth = 82.0; 

@interface MyCustomCell() 
@property (nonatomic, assign) CGFloat firstX; 
@property (nonatomic, assign) CGFloat firstY; 
@property (nonatomic, strong) UIView *topLayer; 
@property (nonatomic, strong) UIButton *moreButton; 
@property (nonatomic, strong) UIButton *lostButton; 
@property (nonatomic, strong) UILabel *titleLabel; 
@end 

@implementation MyCustomCell 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 
    self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier]; 
    if (self) { 

     // BOTTOM LAYER (MORE AND LOST BUTTONS) 
     UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [aButton setFrame:CGRectZero]; 
     [aButton setTitle:@"More" forState:UIControlStateNormal]; 
     [aButton setTitleShadowColor:[UIColor clearColor] forState:UIControlStateNormal]; 
     [aButton setBackgroundColor:[UIColor colorWithRed:0.78 green:0.78 blue:0.78 alpha:1.0]]; 
     [[self contentView] addSubview:aButton]; 
     [self setMoreButton:aButton]; 

     aButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [aButton setFrame:CGRectZero]; 
     [aButton setTitle:@"Lost" forState:UIControlStateNormal]; 
     [aButton setTitleShadowColor:[UIColor clearColor] forState:UIControlStateNormal]; 
     [aButton setBackgroundColor:[UIColor colorWithRed:1.00f green:0.23f blue:0.19f alpha:1.00f]]; 
     [[self contentView] addSubview:aButton]; 
     [self setLostButton:aButton]; 

     // TOP LAYER 
     UIView *aView = [[UIView alloc] initWithFrame:CGRectZero]; 
     [aView setBackgroundColor:[UIColor whiteColor]]; 
     [[self contentView] addSubview:aView]; 
     [self setTopLayer:aView]; 

     UIPanGestureRecognizer *aPanRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureAction:)]; 
     [aPanRecognizer setMinimumNumberOfTouches:1]; 
     [aPanRecognizer setMaximumNumberOfTouches:1]; 
     [aView addGestureRecognizer:aPanRecognizer]; 

     // title label 
     UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectZero]; 
     [aView addSubview:aLabel]; 
     [self setTitleLabel:aLabel]; 
    } 
    return self; 
} 

- (void)layoutSubviews { 

    [super layoutSubviews]; 

    CGRect bounds = [[self contentView] bounds]; 
    CGRect frame = CGRectZero; 

    // BOTTOM LAYER (MORE AND LOST BUTTONS) 
    frame.origin.x = bounds.size.width-(kEditButtonWidth+kEditButtonWidth); // two buttons wide 
    frame.size.width = kEditButtonWidth; 
    frame.size.height = bounds.size.height; 
    [[self moreButton] setFrame:frame]; 

    frame.origin.x += kEditButtonWidth; 
    [[self lostButton] setFrame:frame]; 

    // TOP LAYER 
    frame = bounds; 
    CGPoint anchorPoint = CGPointMake(0.0f, [[[self topLayer] layer] anchorPoint].y); 
    [[[self topLayer] layer] setAnchorPoint:anchorPoint]; 
    [[self topLayer] setFrame:frame]; 

    // title label 
    frame.origin.x = 20.0; 
    frame.origin.y = 4.0; 
    frame.size.width = bounds.size.width-40.0; 
    frame.size.height = 21.0; 
    [[self titleLabel] setFrame:frame]; 
} 

- (void)panGestureAction:(id)sender { 

    // on the first touch, get the center coordinates (x and y) of the view 
    CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self]; 
    if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) { 
     [self setFirstX:[[sender view] center].x]; 
     [self setFirstY:[[sender view] center].y]; 
    } 

    // add translated point to reference points 
    translatedPoint = CGPointMake([self firstX]+translatedPoint.x, [self firstY]); 
    [[sender view] setCenter:translatedPoint]; 

    // when pan ends (set final x to be either back to zero or showing buttons) 
    if ([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) { 
     CGFloat finalX = translatedPoint.x+(0.30*[(UIPanGestureRecognizer*)sender velocityInView:self].x); 
     if (finalX < -1.0f*FMEditButtonWidth) { 
      finalX = -2.0f*FMEditButtonWidth; 
      [self setEditMode:YES]; 
     } else { 
      finalX = 0.0f; 
      [self setEditMode:NO]; 
     } 

     // animate view 
     [UIView animateWithDuration:1.0 delay:0.0 usingSpringWithDamping:1.0f initialSpringVelocity:1.0f options:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState animations:^{ 
      [[sender view] setCenter:CGPointMake(finalX, [self firstY])]; 
     } completion:NULL]; 
    } 
} 

@end 

希望這會有所幫助。

+0

感謝分享!我會在接下來的幾周嘗試一下。 –

+0

我試了一下代碼,但我無法弄清楚,爲什麼手勢無法識別。該方法不會被調用。你能幫忙嗎? – alexdd55

+0

@ alexdd55在initWithStyle中設置了一個斷點:查看它是否觸發並正確地連接了你的手勢。 –

1

您在layoutSubviewswillTransitionToState:中看不到視圖的原因是,刪除按鈕視圖尚未存在,或者它不屬於視圖層次結構的一部分。 爲了「看」它,你需要推遲一點是瀏覽視圖層次結構的代碼,並通過允許操作系統創建/在此期間新增的觀點:

- (void)willTransitionToState:(UITableViewCellStateMask)state 
{ 
    [super willTransitionToState:state]; 

    if (state & UITableViewCellStateShowingDeleteConfirmationMask) 
    { 
     [self performSelector:@selector(findDeleteButtonViewThroughViewHierarchy:) withObject:self.subviews afterDelay:0]; 
    } 
} 

注意這是一個脆弱的解決方案,因爲它基於可能隨時改變的Apple的內部實施。

在這裏看到(幾乎)完整的例子:

Customizing iOS 7 Delete button