2012-09-07 61 views
12

我想知道我在想什麼,因爲我的單元格在隱藏刪除按鈕時沒有動畫。在刪除按鈕完成動畫之前,標籤會跳回原始位置。隱藏刪除按鈕時自定義的uitableviewcell內容不動畫

當我點擊了一輪編輯視圖顯示刪除按鈕,標籤動畫作品:

Screenshot of cells where the delete button appears

然而,當我再次點擊它來隱藏刪除按鈕,標籤的運動是不動畫:

Screenshot of cells where the delete button disappears

注意:這些截圖是不是從下面的代碼創建的。但他們表明了這個問題。

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    homeCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[homeCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
    } 

    // Set up the cell 
    Consumed *drinkObj = [self.appDelegate.consumedDrinksArray objectAtIndex:indexPath.row];   
    cell.titleLabel.text = drinkObj.drinkName; 
    NSString *detailTextTime = [NSDate stringFromDate:drinkObj.dateConsumed withFormat:@"h:mma"]; 

    NSString *detailTextrelative = [relativeDateTransformer transformedValue:drinkObj.dateConsumed]; 

    NSString *detailText = [NSString stringWithFormat:@"%@ %@ ", detailTextTime,detailTextrelative]; 
    cell.timeLabel.text = detailText; 

    cell.stdDLabel.text = @"999.0"; //[NSString stringWithFormat:@"%@", drinkObj.standardDrinks]; 
    cell.stdDLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; 
    cell.titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; 
    cell.timeLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    return cell; 
} 
+1

我有同樣的問題,你的視頻是404,並且有多餘的代碼。我想我改進了你的問題。如果你覺得不同的感覺自由滾動回來。 –

+1

你的'homeCell'類是怎麼樣的?你壓倒一切的方法? – Sulthan

+1

尤其是'layoutSubviews'是否被覆蓋? – Sulthan

回答

6

編輯: 我看到這個bug與我在第一個例子中所理解的不同。 讓我們糾正我的答案。 如果我是正確的,你的刪除按鈕通過動畫正確消失,但是內容視圖沒有動畫(調整按鈕動畫)後自動調整大小。

這種情況下,你必須自己管理動畫。 我給你的代碼假設你不支持輪換。如果你想支持旋轉,你需要更多的代碼:-)

這裏是一個示例項目(有很多代碼,因爲我使用標準的主 - 細節xcode模板,只看到自定義單元格) :Sample Project

將它應用到你的代碼:

第一件事,更改單元格右側標籤的自動尺寸面具錨定在左側。

我不知道如果有合適的標籤是stdDLabel,我會認爲它

// if the right margin is flexible, the left-top-bottom are fixed 
cell.stdDLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin; 

在你的子類中重寫setEditing:animated:方法是這樣的:

-(void)setEditing:(BOOL)editing animated:(BOOL)animated 
{ 
    // this is an array of the labels/view that you want to animate 
    NSArray *objectsToMove = @[self.stdDLabel]; 
    // this is the amount of pixel to move - the width of the delete button 
    float pixelToMove = 70.0f; 

    float animationDuration = 0.3f; 

    // calculating the delta. If set editing, move from right to left. otherwise, from left to right 
    float delta = (editing) ? -pixelToMove : pixelToMove; 

    // put the move code in a block for avoid repeating code in the if 
    void (^moveBlock)() = ^{ 
     for (UIView *view in objectsToMove) { 
      view.center = CGPointMake(view.center.x + delta, view.center.y); 
     } 
    }; 

    // we want to move the labels only if editing is different from the current isEditing state 
    if (editing != self.isEditing) 
    { 
     // execute the move block, animated or not 
     if (animated) 
      [UIView animateWithDuration:animationDuration animations:moveBlock]; 
     else 
      moveBlock(); 
    } 

    // call the super implementation 
    [super setEditing:editing animated:animated]; 
} 
+1

由於我總是處於編輯模式'setEditing:animated:'對我無能爲力,但無論如何這是一個很好的答案。我想我會把這種行爲當作蘋果框架中的一個bug。不會修復,對很少的工作很少有好處。謝謝。 –

+1

你說得對,我並不認爲你總是處於編輯模式。我認爲(稍後會嘗試)你可以解決覆蓋'willTransitionToState:'方法,每次使用類型爲UITableViewCellStateMask的參數 – LombaX

3

我沒有檢查所有的代碼,但是可以肯定你需要添加開始/結束更新上刪除的每一面......

[self.drinksTableView beginUpdates]; 
[self.drinksTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
[self.drinksTableView endUpdates]; 

...到獲取動畫。

+1

嗨丹,謝謝你的答覆。刪除行動畫的作品。 我的意思是當第一次選擇「 - 」到「|」時,動畫就起作用。但是當從「|」到「 - 」,沒有任何動畫。 – Desmond

3

您可以使用gesturerecognizer執行此任務。它可以幫助你管理你的單元格,並在單元格上添加一個custome刪除按鈕,而不是默認的刪除按鈕。

2
tableView:didEndEditingRowAtIndexPath:

通過settign它們的幀

- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    homeCell *cell = (homeCell *)[tableView cellForRowAtIndexpath:indexPath]; 

    UILabel *labelToBeRelocated = (UILabel *)[cell viewWithTag:YOUR_LABEL_TAG]; 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.25f]; 

    [labelToBeRelocated setFrame:CGRectMake(New_PosX , New_PosY , width , height)]; 

    [UIView commitAnimations]; 
} 

由於EditingButton(刪除按鈕)被隱藏在的UITableViewCell後上述委託方法被調用,因此[執行的手動重新定位UILabels任務只有在刪除按鈕被隱藏後,UILabel纔會重新定位它的位置。 希望它能幫助你。

+1

這不起作用。如果標籤是正確錨定的,它將立即跳轉,因爲contentView在沒有動畫的情況下更改其框架(bug?)。如果您將標籤左側定位,則必須在出現按鈕時從右向左移動,並在消失時從左向右移動(使用另一種方法)。查看我的解決方案 – LombaX

相關問題