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
UITableView didSelectRowAtIndexPath: Not Working
提问by JBL
When I select the table row, nothing happens. It didn't go to ContentController and I can't find the UILabel
that 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 == NO
then didSelectRowAtIndexPath
method will never called for that cell.
if clickable == NO
thendidSelectRowAtIndexPath
方法永远不会调用那个单元格。
回答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);
}
回答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 HJManaged
Image in UITableView
cell and HJManaged
Image has its own tap method in its delegate
, so when i tap on table view instead of didSelectRowAtIndexPath
, delegate tap method of HJManaged
Image getting called.
好吧,我有一些不同的场景,我HJManaged
在UITableView
单元格中有Image并且HJManaged
Image 在它的中有自己的 tap 方法delegate
,所以当我点击 table view 而不是 时didSelectRowAtIndexPath
,委托HJManaged
Image 的tap 方法被调用。
So what i found solution , i went back UITableView
Cell , And unchecked User Interaction enabled property of HJManaged
Image.
所以我找到了解决方案,我回到了UITableView
Cell ,并取消了HJManaged
Image 的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 委托。