2011-10-21 7 views
1

これまでにやったことがありますが、もう一度動作させるのに問題があります。いずれにせよ、私のモデルクラスでは、基本的にはソートのための2つの項目を持つ辞書がありますが、私は1つの値だけをYESにし、他のものはNOにします。 UITableViewCellのアイテムの選択を反映させ、選択と選択解除をアニメートする必要があります。だから、モデルクラスで、私はfalseに選択された他の値を変更し、この方法を持っている:UITableView didSelectRowForIndexPathは、チェックマークの選択と選択の解除をアニメーション化しません。

- (void)setSingleSort:(NSString *)key { 
    for (NSString *str in [_sortOrderDictionary allKeys]) { 
     if (str != key) { 
      [_sortOrderDictionary setObject:[NSNumber numberWithBool:NO] forKey:str]; 
     } 
    } 
} 

それから私は私のdidSelectRowForIndexPath方法でこのスニペットを持っている:

NSUInteger row = [indexPath row]; 
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
cell.selectionStyle = UITableViewCellSelectionStyleNone; 
cell.accessoryType = UITableViewCellAccessoryCheckmark; 
[cell setSelected:YES animated:YES]; 

NSString *key; 
BOOL valueAtKey; 
    key = [_sortCategoryList objectAtIndex:row]; 
    valueAtKey = [[dmgr.SortOrderDictionary valueForKey:key] boolValue]; 
    if (valueAtKey != YES) { 
     [dmgr.SortOrderDictionary setObject:[NSNumber numberWithBool:!valueAtKey] forKey:key]; 
     [dmgr setSingleSort:key]; 
     UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:_lastIndexPath]; 
     oldCell.accessoryType = UITableViewCellAccessoryNone; 

     [tableView deselectRowAtIndexPath:_lastIndexPath animated:YES]; 
    } 
    _lastIndexPath = indexPath; 
    [tableView reloadData]; //Edited, added this to the end and it seems to work. Don't know why it's needed though since the deslectRowAtIndexPath should animated, along with the setSelected:Animated: right? 

答えて

-1

この取り払います行:

cell.selectionStyle = UITableViewCellSelectionStyleNone; 
+1

バマー、うまくいきません – Crystal

1

あなたの選択はスタイルだと思います。これは通常、私がこれをやっていく方法です。 cellForRowAtIndexPath方法で:didSelectRowAtIndexPath方法で、その後

//create new or fetch dequeued cell 
if((indexPath.row == self.indexPathOfCurrentValue.row) && (indexPath.section == self.indexPathOfCurrentValue.section)) 
    cell.accessoryType = UITableViewCellAccessoryCheckmark; 
else 
    cell.accessoryType = UITableViewCellAccessoryNone; 

、このような何かを:

[tableView deselectRowAtIndexPath:indexPath animated:YES]; 

//APP RELATED LOGIC GOES HERE 

if(nil != self.indexPathOfCurrentValue) 
    [[tableView cellForRowAtIndexPath:self.indexPathOfCurrentValue] setAccessoryType:UITableViewCellAccessoryNone]; 

self.indexPathOfCurrentValue = indexPath; 
[[tableView cellForRowAtIndexPath:self.indexPathOfCurrentValue] setAccessoryType:UITableViewCellAccessoryCheckMark]; 

これは通常、私が取るアプローチである私は、ユーザーのタップ上の行をチェックマークし、最後のチェックを外す必要がある場合選択された行明らかに、currentValueのインデックスパス変数は、最新のチェックマークのインデックスパスを保持しています。

+0

hm、最後に[tableView reloadData]を実行すればうまくいくようです。この場合、なぜアニメーションが必要なのか分かりません。 – Crystal

関連する問題