UITableView didSelectRowForIndexPath,不动画选择和取消选择复选标记

发布于 2024-12-11 06:37:57 字数 1535 浏览 0 评论 0原文

我以前曾经这样做过,但很难让它再次工作。无论如何,在我的模型类中,我基本上有一个字典,其中有两个用于排序的项目,并且我只希望一个值是“是”,其他值是“否”。我希望 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往日 2024-12-18 06:37:57

我认为你选择的风格就是原因。我通常就是这样做的。在 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 索引路径变量保存最新复选标记的索引路径。

I think your selectionStyle is the reason. This is normally how I go about doing this. In cellForRowAtIndexPath method:

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

then in the didSelectRowAtIndexPath method, do something like this:

[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];

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.

若水微香 2024-12-18 06:37:57

摆脱这一行:

cell.selectionStyle = UITableViewCellSelectionStyleNone;

Get rid of this line:

cell.selectionStyle = UITableViewCellSelectionStyleNone;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文