自定义单元格didSelectRowAtIndexPath未调用

时间:2013-03-13 17:18:08

标签: ios objective-c xcode

我创建了一个通用的主从细节应用程序。我在自定义类(MasterViewController类)中有一个表视图。这是一个UITableViewController类。 使用Storyboard,我在iPhone表视图中创建了一个自定义单元格。自定义单元格包含3个标签和1个图像。 在StoryBoard中,此表视图数据源和委托设置为MasterViewController类。 我的自定义表格视图单元格在属性检查器的“视图”部分中启用了“用户交互”。 在我的MasterViewController类中,诸如'numberOfSectionsInTableView'之类的UITableViewDataSource方法正常工作。 我在表视图和详细视图之间定义了一个seque。 当我运行应用程序并在表视图中选择一个单元格时,不会为iPhone调用didSelectRowAtIndexPath。我的prepareForSegue方法执行并发生segue。

我已经阅读了很多关于didSelectRowAtIndexPath未被执行的帖子,但我无法解决我的问题。

任何提示都会非常感激。

我的自定义单元格标识符是ConversationCell。 segue标识符是ShowConversationDetails。 我在numberOfRowsInSection的第一行使用断点进行调试,并且从不输入此方法。

segue工作,详细视图显示我的标签为“Hello”文字。

部分代码:

- (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: 这是我上面的界面;

@interface MasterViewController : UITableViewController     <NSFetchedResultsControllerDelegate,ASIHTTPRequestDelegate,UITableViewDelegate, UITableViewDataSource>
{
NSMutableArray *eventsArray;

}

这可能是我的问题的线索。当我将以下代码添加到prepareForSeque

NSIndexPath *selectedIndex = [self.tableView indexPathForSelectedRow];
    NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:selectedIndex];
    NSString *messageTableID = [[object valueForKey:@"messagetableid"] description];

selectedIndex为空,就好像在执行prepareForSeque时不再选择行一样。

顺便说一句,为了安全起见,我从模拟器中移除了应用程序,然后对项目执行了清理并且没有任何更改。

3 个答案:

答案 0 :(得分:2)

除了您已经提到的支票之外,还有一些您可能需要考虑的其他支票:

检查方法是否正确(字母大小写没有区别):

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

检查控制器是否实现了UITableViewDelegate协议

检查交互是否有效(更改选择样式选项,以便单元格在触摸事件时更改颜色)

检查您是第一响应者内部没有任何视图并取消交互(删除要测试的单元格中的任何视图)

尝试以编程方式允许在表[tableView setAllowsSelection:YES];

上进行选择

答案 1 :(得分:0)

您确定您的UITableViewDelegate对象是否正确设置为表格视图的delegate? (这与将其设置为数据源分开。)

答案 2 :(得分:0)

我最终删除了我的MasterViewController和DetailViewController。我重新创建它们,现在正在运行。不确定我第一次做错了什么。感谢所有提供建议的人。 添