正确设置 willSelectRowAtIndexPath 和 didSelectRowAtIndexPath 来发送单元格选择

发布于 2024-08-31 14:19:40 字数 1455 浏览 9 评论 0原文

感觉我在这里有点疯狂。我有一个详细视图,其中包含一些独立的 UITextFieldUITAbleViewCell 中的一些 UITextFields 以及一个 UITableViewCell< /code> 将用于保存笔记(如果有的话)。我只希望当我处于编辑模式时可以选择该单元格。当我不处于编辑模式时,我不希望能够选择它。选择单元格(在编辑模式下)将触发一个方法来初始化一个新视图。我知道这很容易,但我在某处遗漏了一些东西。

以下是我当前使用的选择方法:

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!self.editing) {
        NSLog(@"Returning nil, not in edit mode");
        return nil;
    }
    NSLog(@"Cell will be selected, not in edit mode");
    if (indexPath.section == 0) {
        NSLog(@"Comments cell will be selected");
        return indexPath;
    }
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!self.editing) {
        NSLog(@"Not in edit mode. Should not have made it this far.");
        return;
    }

    if (indexPath.section == 0)
        [self pushCommentsView];
    else
        return;
}

我的问题确实是 2 倍;

1)即使我没有处于编辑模式,并且我知道我正在返回 nil(由于 NSLog 消息),我仍然可以选择该行(它会闪烁)蓝色的)。根据我对 willSelectRowAtIndexPath 方法的理解,这种情况不应该发生。也许我错了?

2)当我进入编辑模式时,我根本无法选择任何内容。 willSelectRowAtIndexPath 方法永远不会触发,didSelectRowAtIndexPath 也不会触发。我在 setEditing 方法中做的唯一一件事是在编辑时隐藏后退按钮,并将 firstResponder 分配给顶部文本字段以弹出键盘。我认为也许第一个响应者妨碍了点击(这将是愚蠢的),但即使注释掉了,我也无法在编辑期间执行单元格选择。

Feel like I'm going a bit nutty here. I have a detail view with a few stand-alone UITextFields, a few UITextFields in UITAbleViewCells, and one single UITableViewCell that will be used to hold notes, if there are any. I only want this cell selectable when I am in edit mode. When I am not in edit mode, I do not want to be able to select it. Selecting the cell (while in edit mode) will fire a method that will init a new view. I know this is very easy, but I am missing something somewhere.

Here are the current selection methods I am using:

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!self.editing) {
        NSLog(@"Returning nil, not in edit mode");
        return nil;
    }
    NSLog(@"Cell will be selected, not in edit mode");
    if (indexPath.section == 0) {
        NSLog(@"Comments cell will be selected");
        return indexPath;
    }
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!self.editing) {
        NSLog(@"Not in edit mode. Should not have made it this far.");
        return;
    }

    if (indexPath.section == 0)
        [self pushCommentsView];
    else
        return;
}

My problem is really 2 fold;

1) Even when I'm not in edit mode, and I know I am returning nil (due to the NSLog message), I can still select the row (it flashes blue). From my understanding of the willSelectRowAtIndexPath method, this shouldn't be happening. Maybe I am wrong about this?

2) When I enter edit mode, I can't select anything at all. the willSelectRowAtIndexPath method never fires, and neither does the didSelectRowAtIndexPath. The only thing I am doing in the setEditing method, is hiding the back button while editing, and assigning firstResponder to the top textField to get the keyboard to pop up. I thought maybe the first responder was getting in the way of the click (which would be dumb), but even with that commented out, I cannot perform the cell selection during editing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

愿得七秒忆 2024-09-07 14:19:40

天哪,我是个白痴。我从未添加过这些行:

self.tableView.allowsSelection = NO; // Keeps cells from being selectable while not editing. No more blue flash.
self.tableView.allowsSelectionDuringEditing = YES; // Allows cells to be selectable during edit mode.

抱歉提出了垃圾问题。

Good lord I am an idiot. I never added these lines:

self.tableView.allowsSelection = NO; // Keeps cells from being selectable while not editing. No more blue flash.
self.tableView.allowsSelectionDuringEditing = YES; // Allows cells to be selectable during edit mode.

Sorry for the garbage question.

回忆凄美了谁 2024-09-07 14:19:40

文档指出,在编辑模式下不会调用 tableView:willSelectRowAtIndexPath: 。另外,即使取消选择,蓝色闪烁也会发生。从文档中:

只有当用户触摸一行然后抬起手指时,才会调用此方法;直到那时该行才被选中,尽管它在触地时突出显示。您可以使用 UITableViewCellSelectionStyleNone 禁用触地时单元格突出显示的外观。当表的编辑属性设置为YES(即表视图处于编辑模式)时,不会调用此方法。

The documentation notes that tableView:willSelectRowAtIndexPath: isn't called when in editing mode. In addition, the blue flash will happen even if you cancel the selection. From the documentation:

This method is not called until users touch a row and then lift their finger; the row isn't selected until then, although it is highlighted on touch-down. You can use UITableViewCellSelectionStyleNone to disable the appearance of the cell highlight on touch-down. This method isn’t called when the editing property of the table is set to YES (that is, the table view is in editing mode).

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