2013-08-26 24 views
-3

我遇到了問題,我的tableView沒有觸發didSelectRowAtIndexPath方法。我已經實現了這樣的代表: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),那麼當在用戶點擊按鈕在導航欄中,視圖滑過以顯示它,這可能會導致我認爲的問題。

我的代碼或實現有什麼問題會導致這不起作用?

+0

大概是一個明顯的問題,但你肯定你已經同時設置了代理和的tableView的數據源? – cobbal

+0

@cobbal是啊我將它們連接到視圖控制器的故事板,並在.h文件中實現它們 – gta0004

+0

在您的didSelectRowAtIndexPath方法中添加'[tableView deselectRowAtIndexPath:indexPath];''。如果在您擡起手指時取消選擇該行,問題出在您的'changeImage:'方法中。 – Jsdodgers

回答

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; 

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

這將情況返回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"]; 
} 
相關問題