didSelectRowAtIndexPath不起作用

时间:2013-08-26 19:00:02

标签: ios objective-c cocoa-touch uitableview

我遇到的问题是我的tableView没有触发didSelectRowAtIndexPath方法。我已经实施了这样的代表:

@interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate,UIScrollViewDelegate>

在我的故事板中,tableView的数据源和委托都指向基本的View Controller。我启用了用户交互以及选择设置为单选,并且它不是TapGesture问题,因为我的点击手势未绑定到视图并且我已经检查并且它们不会触发。

这是设置表格的代码:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return menuArray.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"menuCell"];
    NSDictionary *menuItem = [menuArray objectAtIndex:indexPath.row];

    cell.textLabel.text = menuItem[@"Title"];
    cell.detailTextLabel.text = menuItem[@"Subtitle"];
    return cell;
}
-(void)showMenu{
    [UIView animateWithDuration:.25 animations:^{
        [content setFrame:CGRectMake(menuTable.frame.size.width, content.frame.origin.y, content.frame.size.width, content.frame.size.height)];
    }];
}
-(void)hideMenu{
    [UIView animateWithDuration:.25 animations:^{
    [content setFrame:CGRectMake(0, content.frame.origin.y, content.frame.size.width,     content.frame.size.height)];
    }];
}
-(IBAction)showMenuDown:(id)sender {
    if(content.frame.origin.x == 0)
        [self showMenu];
    else
        [self hideMenu];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //whatever
}

该表最初在故事板上不可见(origin.x设置为-150),然后当用户点击navigationBar中的按钮时,视图滑过以显示它,这可能是导致我认为这个问题。

我的代码或实现是否有任何问题导致其无法正常工作?

2 个答案:

答案 0 :(得分:0)

如果您已经看到您的表中填充了字典中的值,那么您可以排除数据源并委托作为问题。即你的故事板连接正在运作。

您的代码对我来说很好。我看到的唯一区别是我通常像这样定义我的表。试试这个,看看它是否有帮助。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    //NSLog(@"Inside cellForRowAtIndexPath");

    static NSString *CellIdentifier = @"Cell";

    // Try to retrieve from the table view a now-unused cell with the given identifier.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // If no cell is available, create a new one using the given identifier.
    if (cell == nil)
    {
        // Use the default cell style.
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

//Your code here
// ....

return cell;

}

答案 1 :(得分:0)

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"menuCell"];

如果从未创建过单元格,则返回nil。

所以检查cell是否为nil是强制性的,如果是,则需要创建一个单元格。

static NSString *CellIdentifier = @"menuCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
}

当您使用故事板时,您也可以使用

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

原型细胞。确保在故事板中使用相同的标识符并注册了单元格的类

- (void) viewDidLoad {
   [super viewDidLoad];

   [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"menuCell"];
} 
相关问题