xcode UITableView didSelectRowAtIndexPath:不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8178845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:28:11  来源:igfitidea点击:

UITableView didSelectRowAtIndexPath: Not Working

iphoneobjective-ciosxcodeuitableview

提问by JBL

When I select the table row, nothing happens. It didn't go to ContentController and I can't find the UILabelthat I declared on ContentController.h when I want to link it to resultLabel.

当我选择表格行时,没有任何反应。它没有转到 ContentController,UILabel当我想将它链接到 .h 时,我找不到我在 ContentController.h 上声明的resultLabel

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ContentController *detailview = [[ContentController alloc] initWithNibName:@"ContentController" bundle:nil];    
    detailview.detailString = [NSString stringWithFormat:@"%d",indexPath.row];
    [self.navigationController pushViewController:detailview animated:YES];     
    [detailview release];
}

ContentController

内容控制器

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    resultLabel.text = self.detailString;
}

回答by Aakil Ladhani

This may be because you don't have set your table view property to delegate & datasource by:

这可能是因为您没有通过以下方式将表视图属性设置为委托和数据源:

tableview.delegate=self;
tableview.datasource=self;

or set their property in xib is another option

或在 xib 中设置他们的属性是另一种选择

回答by evya

clickable == NO

可点击 == 否

Can be a state that in the method cellForRowAtIndexPath:

可以是方法中的状态cellForRowAtIndexPath

cell = [self tableCellWithHeight:height clickable:NO withArrowAccessory:NO];

if clickable == NOthen didSelectRowAtIndexPathmethod will never called for that cell.

if clickable == NOthendidSelectRowAtIndexPath方法永远不会调用那个单元格。

回答by IbrarMumtaz

Another thing to check is to make sure the prototype cells and their selection mode or selection style.

要检查的另一件事是确保原型单元格及其选择模式或选择样式。

Goto interface builder >>> utility windows on the right hand side >>> attributes inspector >>> Look for Selection drop down list.

转到界面生成器 >>> 右侧的实用程序窗口 >>> 属性检查器 >>> 查找选择下拉列表。

In the apple tutorial I was following, it asks you to set it to none, making it look like that the didSelect' delegate was not firing. Setting it the selection mode to none, effectively neutered this ability. I changed this to Single Selection and it all started to work again.

在我关注的苹果教程中,它要求您将其设置为 none,使其看起来像 didSelect' 委托没有触发。将它的选择模式设置为无,有效地消除了这种能力。我把它改成了单选,然后一切又开始工作了。

回答by SoftwareDeveloper

Check if you are implement this method

检查您是否正在实施此方法

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath: (NSIndexPath *)indexPath
{
     return NO;
}

it disable the cell selection

它禁用单元格选择

回答by Devendra Agnihotri

This occurs may be you have not add your table view delegate and data source.Please ensure and add these two lines in your view did load methods

发生这种情况可能是您没有添加您的表视图委托和数据源。请确保并在您的视图中添加这两行确实加载方法

     tableview.delegate=self;
     tableview.datasource=self;

回答by SuperBi

My situation is I forgot to set Delegate propertyof the tableView. Maybe you should connect tableView delegate to current view controller.

我的情况是我忘记设置tableView 的Delegate 属性。也许您应该将 tableView 委托连接到当前视图控制器。

回答by MGM

Here is an image of setting the datasource and delegate in interface builder for UITableview which is necessary for the method below to fire on touch.

这是在 UITableview 的界面构建器中设置数据源和委托的图像,这是以下方法在触摸时触发所必需的。

..

..

                - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
                {
                    return 1;
                }

                - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
                {
                    return [self.myAlphabet count];
                }

                - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
                {
                    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];    
                    cell.textLabel.text = [self.myAlphabet objectAtIndex:indexPath.row];
                    return cell;
                }

                - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
                {
                    //method for getting text (can be changed to push views)
                    NSString*mystring;
                    mystring = [self.tableView cellForRowAtIndexPath:indexPath].textLabel.text;
                    NSLog(@"%@",mystring);    
                }

Setting datasource and delegate in interface builder

在界面构建器中设置数据源和委托

回答by evya

When creating cell (on cellForRowAtIndexPath) should notice to:

创建单元格时(在 cellForRowAtIndexPath 上)应注意:

userInteractionEnabled propertyDetermine if cell can be selected (on click should -didSelectRowAtIndexPath will call).

userInteractionEnabled 属性确定是否可以选择单元格(单击时 -didSelectRowAtIndexPath 将调用)。

if (!isClickable) {
    cell.userInteractionEnabled = NO;
}

selectionStyle property:Determine if selected backround view will show on click.

selectionStyle 属性:确定选定的背景视图是否会在单击时显示。

cell.selectionStyle = isClickable ? UITableViewCellSelectionStyleBlue : UITableViewCellSelectionStyleNone;

回答by Hasya

Well , i had some different scenario , i am having HJManagedImage in UITableViewcell and HJManagedImage has its own tap method in its delegate, so when i tap on table view instead of didSelectRowAtIndexPath, delegate tap method of HJManagedImage getting called.

好吧,我有一些不同的场景,我HJManagedUITableView单元格中有Image并且HJManagedImage 在它的中有自己的 tap 方法delegate,所以当我点击 table view 而不是 时didSelectRowAtIndexPath,委托HJManagedImage 的tap 方法被调用。

So what i found solution , i went back UITableViewCell , And unchecked User Interaction enabled property of HJManagedImage.

所以我找到了解决方案,我回到了UITableViewCell ,并取消了HJManagedImage 的User Interaction enabled 属性。

回答by Minakshi

Check is control is coming inside didSelectRowAtIndexPath applying breakpoint. If not just check whether you have set the UITableView delegate or not.

检查控制是否进入 didSelectRowAtIndexPath 应用断点。如果不是,请检查您是否已设置 UITableView 委托。