2013-03-29 81 views
0

我有自定義單元格選擇時執行一個簡短的動畫,並應取消選擇時動畫回到其第一個狀態。UITableView deselectRowAtIndexPath調用UITableviewCell setSelected?

這裏是我setSelected

- (void)setSelected:(BOOL)selected animated:(BOOL)animated { 

if (selected && animated) { 
    NSLog(@"animate"); 
    [UIView animateWithDuration:0.3 
          delay:0.0 
         options:UIViewAnimationOptionBeginFromCurrentState 
        animations:^{ 
         self.chevronImage.transform = CGAffineTransformMakeRotation(M_PI); 
         [self.chevronImage setCenter:CGPointMake(self.chevronImage.center.x, self.chevronImage.center.y - 1)]; 
        } 
        completion:nil]; 
} 

if (!selected && animated) { 
    NSLog(@"unanimate"); 
    [UIView animateWithDuration:0.3 
          delay:0.0 
         options:UIViewAnimationOptionBeginFromCurrentState 
        animations:^{ 
         self.chevronImage.transform = CGAffineTransformMakeRotation(0); 
         [self.chevronImage setCenter:CGPointMake(self.chevronImage.center.x, self.chevronImage.center.y + 1)]; 
        } 
        completion:nil]; 
} 

[super setSelected:selected animated:animated]; 
} 

這裏是調用它的代碼:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

if (![selectedIndex isEqual:indexPath]) { 
    NSLog(@"select %i", indexPath.row); 
    [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; 
} 
if (selectedIndex != nil) { 
    NSLog(@"deselect %i", selectedIndex.row); 
    [tableView deselectRowAtIndexPath:selectedIndex animated:YES]; 
} 
if (controlRowIndex != nil && [indexPath isEqual:controlRowIndex]) { 
    return; 
} 

indexPath = [self modelIndexPathforIndexPath:indexPath]; 
NSIndexPath *indexPathToDelete = controlRowIndex; 

if ([indexPath isEqual:selectedIndex]){ 
    selectedIndex = nil; 
    controlRowIndex = nil; 
} else { 
    selectedIndex = indexPath; 
    controlRowIndex = [NSIndexPath indexPathForRow:indexPath.row + 1 
             inSection:indexPath.section]; 
} 

[self.tableView beginUpdates]; 
if (indexPathToDelete){ 
    [self.tableView deleteRowsAtIndexPaths:@[indexPathToDelete] 
          withRowAnimation:UITableViewRowAnimationFade]; 
} 
if (controlRowIndex){ 
    [self.tableView insertRowsAtIndexPaths:@[controlRowIndex] 
          withRowAnimation:UITableViewRowAnimationFade]; 
} 
[self.tableView endUpdates]; 
} 

,如果我選擇了某行,然後再次點擊以取消它也能正常工作。但是,如果我點擊第0行,然後點擊第1行,我會選擇兩行,第0行不會被取消選擇。

我想不出有什麼不同,我每次都傳遞完全相同的值,但是如果它當前也被選中,我只能將它設置爲動畫。我已經驗證它發送了正確的indexPath,所以我有點不知所措。

編輯:添加完整的方法代碼

+0

只有使用[標籤:Xcode中]標籤有關IDE的問題。 [tag:objective-c]標籤相同。謝謝! – Undo

回答

0

我本來期望您設置在didSelectRowAtIndexPath:變量selectedIndex,但我看不到你在哪裏設置它。因此,看起來您正在比較錯誤的索引路徑。

從理論上講,我會先取消選擇。

BOOL tappedSelectedCell = [selectedIndex isEqual:indexPath]; 
if (selectedIndex != nil) { 
    // deselect selectedIndex 
    selectedIndex = nil; 
} 

if (selectedIndex == nil && !tappedSelectedCell) { 
    // select indexPath 
    selectedIndex = indexPath; 
} 

或簡單,但稍長

if (selectedIndex == nil) { 
    // select indexPath 
    selectedIndex = indexPath; 
    return; 
} 

if ([selectedIndex isEqual:indexPath]) { 
    // deselect indexPath 
    selectedIndex = nil; 
} 
else { 
    // deselect selectedIndex 
    // select indexPath 
    selectedIndex = indexPath; 
} 
+0

我確實將它設置在那裏,我只是沒有發佈整個方法,因爲它做了很多不適用於我的問題的東西(我在所選的一個下方插入了一行)。我將編輯以顯示整個方法。 – hokiewalrus

相關問題