简体   繁体   English

无法在UITableViewController中选择UITableView的行

[英]Unable to select rows of UITableView in UITableViewController

I have created an app using a storyboard . 我使用storyboard创建了一个应用程序。 In other screens, I have used a UITableViewController directly and the selection is working as expected. 在其他屏幕上,我直接使用了UITableViewController ,并且选择工作正常。

In this case, I have a UITableView that is one of several controls within a UIViewController . 在这种情况下,我有一个UITableView ,它是UIViewController的几个控件之一。

My custom ViewController.h file has a definition similar to the below: 我的自定义ViewController.h文件的定义类似于以下内容:

@interface MyViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
    @property (weak,nonatomic) IBOutlet UITableView *myTableView;
@end

Then within viewDidLoad I am doing this: 然后在viewDidLoad我这样做:

_myTableView.delegate = self;
_myTableView.dataSource = self;

Having done this, my numberOfSectionsInTableView , numberOfRowsInSection and cellForRowAtIndexPath methods are all being called and my table looks as I want it to. 完成此操作后,我的numberOfSectionsInTableViewnumberOfRowsInSectioncellForRowAtIndexPath方法都被调用,并且我的表看起来像我想要的那样。

The problem I have is that the rows do not select and the didSelectRowAtIndexPath method is not getting called. 我的问题是行没有选择,并且didSelectRowAtIndexPath方法没有被调用。

I have checked that Selection is set to Single Selection in the storyboard view and I have also tried to set _myTableView.allowsSelection=YES; 我已经检查了故事板视图中的“选择”设置为“单选”,并且我还尝试设置_myTableView.allowsSelection=YES; in viewDidLoad but this doesn't seem to make any difference. viewDidLoad但这似乎没有任何区别。

I know that this is probably something to do with the fact that my table is within a normal view controller, but I can't figure out the magic step I've missed to make the selection work. 我知道这可能与我的表位于普通视图控制器中有关,但我无法弄清楚为使选择工作而错过的神奇步骤。

For now I have added a workaround. 现在,我添加了一种解决方法。 In cellForRowAtIndexPath I have attached a UITapGestureRecognizer to each view in the cell: cellForRowAtIndexPath我已将UITapGestureRecognizer附加到单元格中的每个视图:

for(UIView *view in cell.contentView.subviews){
    [view setUserInteractionEnabled:YES];
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRow:)];
    [tap setNumberOfTapsRequired:1];
    [view addGestureRecognizer:tap];
    view.tag = row; // So that I can identify in the handler which row has been tapped
}

Then in the handler: 然后在处理程序中:

-(void)tapRow:(id)sender{
    UITapGestureRecognizer *gesture = (UITapGestureRecognizer*)sender;
    UIView *myView = (UIView*)gesture.view;
    int row = myView.tag;
    // Handle tap of row here
}

This achieves what I need, but I would still like to figure out what I've done wrong with the row selection! 这样就达到了我所需要的,但是我仍然想弄清楚我在行选择方面做错了什么!

Make sure there is not another view on top of the table view (eg a transparent view that would be stealing touch events). 确保表格视图上方没有其他视图(例如,将窃取触摸事件的透明视图)。 Also ensure that userInteraction is enabled for the tableview and all of its parent views. 还要确保为表视图及其所有父视图启用了userInteraction。

Just incase anyone still has this problem. 以防万一有人仍然有这个问题。 I just realised -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method won't work for any view or subview with a tap gesture recogniser set to it. 我刚刚意识到-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath方法不适用于设置了轻-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath手势识别器的任何视图或子视图。 So remove the tap gesture recogniser from that view of it's parent view. 因此,从其父视图的该视图中删除点击手势识别器。 if you still want to handle touch events, then use "touchesBegan" function. 如果您仍然想处理触摸事件,请使用“ touchesBegan”功能。 ` - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesBegan:touches withEvent:event]; `-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {[super touchesBegan:touches withEvent:event];

UITouch *touch = [touches anyObject]; UITouch * touch = [触摸anyObject];

}` }`

We type sometime wrong delegate call didDeselectRowAtIndexPath instead of didSeselectRowAtIndexPath. 我们有时输入错误的委托调用didDeselectRowAtIndexPath而不是didSeselectRowAtIndexPath。 Is it your case? 是你的情况吗?

I see that you can update your content in the tableview so the connection should be OK. 我看到您可以在表格视图中更新内容,因此连接应该正常。 I think about mistakes with function names. 我考虑函数名称的错误。

Just to test: Replace your didSelectRowAtIndexPath function with this (copy, paste). 只是为了测试:以此替换您的didSelectRowAtIndexPath函数(复制,粘贴)。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"Selected %d",indexPath.row);
}

I had the exactly the same problem with my current project for two minutes yesterday when I realised that I didn't link my delegate and dataSource of the table view. 昨天,当我意识到没有链接表视图的委托和dataSource时,我在当前项目中遇到了完全相同的问题,持续了两分钟。 You made it programmatically, so it should work. 您是通过编程方式制作的,因此应该可以使用。 For me was a delegation problem, you can check your Attributes Inspector to make sure that everything is ok. 对我来说,这是一个委派问题,您可以检查Attributes Inspector以确保一切正常。

In cellForRowAtIndexPath method add this line 在cellForRowAtIndexPath方法中添加此行

cell.userInteractionEnabled = NO; cell.userInteractionEnabled = NO;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM