xcode 未调用自定义单元格 didSelectRowAtIndexPath
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15392106/
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
custom cell didSelectRowAtIndexPath not called
提问by T Davey
I created a universal master-detail application. I have a table view within a custom class (MasterViewController class). This is a UITableViewController class. Using Storyboard, I created a custom cell in my iPhone table view. The custom cell contains 3 labels and 1 image. In StoryBoard, this table view datasource and delegate is set to the MasterViewController class. My custom table view cell has 'User Interaction Enabled' in the View section of the attribute inspector. In my MasterViewController class, the UITableViewDataSource methods such as 'numberOfSectionsInTableView' are working fine. I have a seque defined between the table view and a detailed view. When I run the application and select a cell in the table view, the didSelectRowAtIndexPath is not called for the iPhone. My prepareForSegue method executes and the segue occurs.
我创建了一个通用的主从应用程序。我在自定义类(MasterViewController 类)中有一个表视图。这是一个 UITableViewController 类。使用 Storyboard,我在 iPhone 表格视图中创建了一个自定义单元格。自定义单元格包含 3 个标签和 1 个图像。在 StoryBoard 中,这个表视图数据源和委托被设置为 MasterViewController 类。我的自定义表格视图单元格在属性检查器的“视图”部分具有“启用用户交互”。在我的 MasterViewController 类中,诸如“numberOfSectionsInTableView”之类的 UITableViewDataSource 方法工作正常。我在表视图和详细视图之间定义了一个序列。当我运行应用程序并在表格视图中选择一个单元格时,不会为 iPhone 调用 didSelectRowAtIndexPath。
I have read quite a number of posts on didSelectRowAtIndexPath not being executed, but I have not been able to solve my problem.
我已经阅读了很多关于 didSelectRowAtIndexPath 没有被执行的帖子,但我一直没能解决我的问题。
Any hints would be very much appreciated.
任何提示将不胜感激。
My custom cell identifier is ConversationCell. The segue identifier is ShowConversationDetails. I debug with a breakpoint on the first line of numberOfRowsInSection and this method is never entered.
我的自定义单元标识符是 ConversationCell。转场标识符是 ShowConversationDetails。我在 numberOfRowsInSection 的第一行使用断点进行调试,并且从未输入此方法。
The segue works and the detail view displays my label with 'Hello' text.
segue 工作,详细视图显示带有“Hello”文本的标签。
Some Code:
一些代码:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section];
//NSLog(@"Rows %lu",(unsigned long)[sectionInfo numberOfObjects]);
return [sectionInfo numberOfObjects];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ConversationCell";
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
[[self.fetchedResultsController sections] count]);
NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
self.detailViewController.detailItem = object;
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"ShowConversationDetails"]) {
NSIndexPath *selectedIndex = [self.tableView indexPathForSelectedRow];
Event *e = nil;
e = [self.fetchedResultsController objectAtIndexPath:selectedIndex];
UIViewController *destinationViewController = [segue destinationViewController];
[segue.destinationViewController setLabel:@"Hello"];
}
}
MASTERVIEWCONTROLLER: Here is my interface for above;
MASTERVIEWCONTROLLER:这是我上面的界面;
@interface MasterViewController : UITableViewController <NSFetchedResultsControllerDelegate,ASIHTTPRequestDelegate,UITableViewDelegate, UITableViewDataSource>
{
NSMutableArray *eventsArray;
}
This may be a clue to my problem. When I add the following code to prepareForSeque
这可能是我的问题的线索。当我将以下代码添加到 prepareForSeque 时
NSIndexPath *selectedIndex = [self.tableView indexPathForSelectedRow];
NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:selectedIndex];
NSString *messageTableID = [[object valueForKey:@"messagetableid"] description];
selectedIndex is empty as if a row is no longer selected when prepareForSeque is executing.
selectedIndex 为空,就像在 prepareForSeque 执行时不再选择行一样。
By the way, just to be safe, I removed the app from the simulator and then did a Clean on the project and nothing changed.
顺便说一句,为了安全起见,我从模拟器中删除了该应用程序,然后对项目进行了清理,但没有任何变化。
Tim
蒂姆
回答by David
In addition to the checks you have already mentioned, here are a few other ones you might want to consider:
除了您已经提到的检查之外,以下是您可能需要考虑的其他一些检查:
Check that the method is exactly this (no difference in letter cases) :
检查该方法是否正是这样(字母大小写没有区别):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Check that the controller implements the UITableViewDelegate
protocol
检查控制器是否实现了UITableViewDelegate
协议
Check that interaction does work (change the selection style option so that the cell changes color on touch events)
检查交互是否有效(更改选择样式选项,以便单元格在触摸事件上更改颜色)
Check that you don't have any view inside that is first responder and takes away the interaction (remove any view in the cell to test)
检查您在第一响应者内部没有任何视图并取消交互(删除单元格中的任何视图以进行测试)
Try to programmatically allow selection on the table [tableView setAllowsSelection:YES];
尝试以编程方式允许在表上进行选择 [tableView setAllowsSelection:YES];
回答by bdesham
Are you sure that your UITableViewDelegate
object is correctly being set as the table view's delegate
? (This is separate from setting it as the data source.)
您确定您的UITableViewDelegate
对象被正确设置为表格视图delegate
吗?(这与将其设置为数据源是分开的。)
回答by T Davey
I ended up deleting my MasterViewController and DetailViewController. I recreated them and things are now working. Not sure what I did wrong the first time. Thanks for everyone that offered advice. Tim
我最终删除了我的 MasterViewController 和 DetailViewController。我重新创建了它们,现在一切正常。不确定我第一次做错了什么。感谢所有提供建议的人。蒂姆