UITableView didSelectRowForIndexPath,不动画选择和取消选择复选标记
我以前曾经这样做过,但很难让它再次工作。无论如何,在我的模型类中,我基本上有一个字典,其中有两个用于排序的项目,并且我只希望一个值是“是”,其他值是“否”。我希望 UITableViewCell 中项目的选择能够反映这一点,并为选择和取消选择设置动画。因此,在模型类中,我有这个方法,只需将选择的其他值更改为 false:
- (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?
I've done this before, but am having trouble getting it to work again. In any case, at my model class I basically have a dictionary that has two items for Sorting, and I want only one value to be YES, and the others to be NO. I want the selection of the item in the UITableViewCell to reflect that and animate the selection and deselection. So at the model class, I have this method that just changes the other value that was selected to false:
- (void)setSingleSort:(NSString *)key {
for (NSString *str in [_sortOrderDictionary allKeys]) {
if (str != key) {
[_sortOrderDictionary setObject:[NSNumber numberWithBool:NO] forKey:str];
}
}
}
Then I have this snippet in my didSelectRowForIndexPath method:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你选择的风格就是原因。我通常就是这样做的。在
cellForRowAtIndexPath
方法中:然后在
didSelectRowAtIndexPath
方法中,执行如下操作:当我需要在用户点击时选中该行并取消选中最后一个时,我通常会采用这种方法选定的行。显然,currentValue 索引路径变量保存最新复选标记的索引路径。
I think your selectionStyle is the reason. This is normally how I go about doing this. In
cellForRowAtIndexPath
method:then in the
didSelectRowAtIndexPath
method, do something like this:This is normally the approach I take when I need to checkmark the row on user's tap and uncheck the last selected row. Obviously, the currentValue index path variables holds the lastest checkmark's index path.
摆脱这一行:
Get rid of this line: