正确设置 willSelectRowAtIndexPath 和 didSelectRowAtIndexPath 来发送单元格选择
感觉我在这里有点疯狂。我有一个详细视图,其中包含一些独立的 UITextField
、UITAbleViewCell
中的一些 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 UITextField
s, a few UITextFields
in UITAbleViewCell
s, 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
天哪,我是个白痴。我从未添加过这些行:
抱歉提出了垃圾问题。
Good lord I am an idiot. I never added these lines:
Sorry for the garbage question.
文档指出,在编辑模式下不会调用
tableView:willSelectRowAtIndexPath:
。另外,即使取消选择,蓝色闪烁也会发生。从文档中: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: