2013-02-04 20 views
0

我有一個簡單的tableView。如果我點擊標籤(左側),didSelectRowAtIndexPath會觸發。當我點擊右側(textField)時它不會觸發。UITableView - didSelectRowAtIndexPath只在點擊標籤時觸發

我想不通,我錯過了什麼。我的代碼來創建和選擇單元格如下:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 

     UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 150, 30)]; 
     textField.delegate = self; 
     textField.returnKeyType = UIReturnKeyDone; 
     textField.borderStyle = UITextBorderStyleNone; 
     cell.accessoryView = textField; 
     textField.text = [NSString stringWithFormat:@"%d ", indexPath.row]; 
    } 

    cell.textLabel.text = [NSString stringWithFormat:@"Line %d", indexPath.row]; 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSLog(@"%s", __FUNCTION__); 
    currentIndexPath = indexPath; 
    NSLog(@"didSelect currentIndexPath.section is %i", currentIndexPath.section); 
    NSLog(@"didSelect currentIndexPath.row is %i", currentIndexPath.row); 
} 

所有幫助表示讚賞。

回答

2

我很確定這是設計。在行內的UITextField內輕敲不會自動選擇該行。如果行有附件按鈕,tapping them doesn't select the row, either.

編輯

如果你想知道包含活動文本字段的TableViewCell的索引路徑,將它添加到您的textFieldShould/DidBeginEditing:

id cellContainingFirstResponder = textField.superview.superview ; 
NSIndexPath* indexPathContainingFirstResponder = [self.tableView indexPathForCell:cellContainingFirstResponder] ; 
+0

嗨John John試圖幫助我。但selectRowAtIndexPath不允許我標識該行。我已經在textFieldShould和textFieldDidBeginEditing中嘗試過了。構建自定義單元格的過程如何? –

+0

看到我的第一個編輯。這是否回答你的問題? –

+0

約翰,謝謝 - 我記錄cellContainingFirstResponder但它返回NULL。 –

相關問題