2011-10-21 18 views
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

我觉得你的selectionStyle是原因。这通常是我如何去做这件事。在cellForRowAtIndexPath方法:

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

然后,做这样的事情:

[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

嗯,如果我只是做一个[tableView reloadData]在最后,它似乎工作。不知道为什么它需要,虽然在这种情况下,因为动画应该正常工作? – Crystal

相关问题