2011-07-05 13 views
13

私はほとんどUITableViewCellUITextFieldを実装しています。UITableViewCellでUITextFieldを使用すると、UITableView行を選択できなくなる

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Cell"]; 
    [cell setSelectionStyle:UITableViewCellSelectionStyleBlue]; 
    amountField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 190, 30)]; 
    amountField.placeholder = @"Enter amount"; 
    amountField.keyboardType = UIKeyboardTypeDecimalPad; 
    amountField.textAlignment = UITextAlignmentRight; 
    amountField.clearButtonMode = UITextFieldViewModeNever; 
    [amountField setDelegate:self]; 

    [[cell textLabel] setText:@"Amount"]; 
    [cell addSubview:amountField]; 
    return cell; 
} 

をそして私はまた、他のフィールドの入力ビューを示す許可するようにTextFieldを辞任、didSelectRowメソッドを実装し:むしろ、私はそれを簡単な方法を実装しているCGRectMakeを通って、UITableViewCell.contentView行きます。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    ... 
    [amountField resignFirstResponder]; 
    ... 
} 

これは私のUITextFieldと1はありませんが、私はフィールドを意味し、それらの他の人は全体のセルが選択され、選択された場合、唯一のことは、表内の他の行があるでスムーズに動作し、ブルーに変わりが選択され、テキストを入力できますが、セルは選択されていません。 私はそれをテストし、問題はラインである考え出した:

[cell addSubview:amountField]; 

選択可能な細胞の挙動を壊すようだ、とさえ[cell contentView]に追加すると、この問題を解決しません。私は何か見落としてますか?テキストフィールドがYESからuserInteractionEnabledセットを持っており、それがセル全体を占める場合

答えて

39

は、あなたはセルをタッチして聞くことを得ることができません。セルがタッチに反応するようにするには、テキストフィールドのuserInteractionEnabledNOに設定する必要があります。

編集:そして、あなたが編集可能なテキストフィールドを作成したい場合は、セルが選択されている場合、didSelectRowAtIndexPathに次のコードを追加します、方法

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

    // get the reference to the text field 
    [textField setUserInteractionEnabled:YES]; 
    [textField becomeFirstResponder]; 
} 
+0

を動作しますが、これを試してみてください;'このUITextFieldが編集不可能なレンダリングされないのでしょうか? –

+0

明らかにそうではありません:EmptyStackのアドバイスに従って、[amountField setUserInteractionEnabled:NO]を設定しました。 DidSelectRowメソッドの冒頭で、[amountField setUserInteractionEnabled:YES]を選択し、選択した行がテキストフィールドを持つ行である場合に限ります。選択は現在OKですが、実際にテキストを入力するには、行を2回タップする必要があります。 –

+0

@micpringle、Ofcourse。テキストフィールドがセルを満たすので、テキストフィールドとセルの両方がタッチに反応することはできません。 – EmptyStack

6

をあなたはが壊れていませんでした UITextFieldはUITableViewCellより先にタッチをキャプチャしています。これをテストするには、UITextFieldの外側をタップして、UITableViewCellの境界内でテストします。実際には、期待どおりに選択します。

これを回避するには、UITextFieldをサブクラス化して、UITableViewプロパティを追加します。 UITextFieldをインスタンス化してセルに追加するときにプロパティを設定します。

amountField.tableView = tableView; 

次に、あなたのサブクラスで becomeFirstResponder をオーバーライドする必要があるだろう、との方法でのUITextFieldとセルの行を取得し、手動で

- (BOOL)becomeFirstResponder 
{ 
    // Get the rect of the UITextView in the UITableView's coordinate system 
    CGRect position = [self convertRect:self.frame toView:self.tableView]; 
    // Ask the UITableView for all the rows in that rect, in this case it should be 1 
    NSArray *indexPaths = [self.tableView indexPathsForRowsInRect:position]; 
    // Then manually select it 
    [self.tableView selectRowAtIndexPath:[indexPaths objectAtIndex:0] animated:YES scrollPosition:UITableViewScrollPositionNone]; 
    return YES; 
} 
+0

あなたは絶対に正しいです、私は試しました、それは本当です。ありがとう。 –

+0

これはうまくいくが、多くの追加コードが必要だったが、以下にEmptyStackによって提案されているように、テキストフィールドuserInteractionEnabled = NOを設定するのは2行のコードだった。あなたの助けを借りて、それは問題をより明確にしました。 –

+0

これは保持サイクルではありませんか? 'tableView' - >' visibleCells' - > 'amountField' - >' tableView' – jakenberg

1

最初の事はあなたがあるそれを選択します再利用可能な細胞を使用しなかった。あなたが提供したコーディングは、多くのメモリを引き起こします。

次に、テキストフィールド以外の領域をタッチして行を選択できます。あなたのQUESため

一つの解決策は、あなたのテキストフィールドで

ある代理人

UITableViewCell *cell = (UITableViewCell *)[textField superView]; 
cell.selected=YES; //I think this will call the didSelectRowATIndex; 

私はこれが動作することを確認していません。しかし、試してみる価値がある。

+0

このテーブルは素晴らしい入力マスクなので4つのセルしかないので、メモリは大きな問題ではないはずですが、なぜ私は再利用可能なセルを使用していないと言っていますか?私は思った。私は今あなたの解決策を試みるつもりです。ありがとう。 –

+0

私はあなたが再利用可能な概念を誤解していると思います。http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.htmlを参照してください...とにかく4セルは大丈夫ですが、 10以上の行で問題が発生します。 – iPrabu

+0

私はもちろん間違って理解しているかもしれませんが、細胞が異なるコントロールを持っているときに細胞を再利用するのは避けてください。とにかくリンクのためにありがとう:) –

1

まず、cellForRowAtIndexPathにreuseIdentifierを使用する必要がありました。reuseIdentifier isを使用しない場合、理由:上下にスクロールすると、常に新しいセルと新しいテキストフィールドが割り当てられ、cell == nil conditionので、改訂されたコードはここにある:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *reuseIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; 
if (cell==nil) { 
    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier] autorelease]; 

    UITextField *amountField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 190, 30)]; 
    amountField.placeholder = @"Enter amount"; 
    amountField.keyboardType = UIKeyboardTypeDecimalPad; 
    amountField.textAlignment = UITextAlignmentRight; 
    amountField.clearButtonMode = UITextFieldViewModeNever; 
    [amountField setDelegate:self]; 
    [cell.contentView addSubview:amountField]; 
} 
[cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
[[cell textLabel] setText:@"Amount"]; 

return cell; 
} 

didSelectのデリゲートメソッドでは、太字で示される行を追加したインデックスパスで行のセル内で、この

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [prevField resignFirstResponder]; 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    UIView *view = [[cell.contentView subviews] lastObject]; 
    if([view isKindOfClass:[UITextField class]]{ 
    currentField = (UITextField*)view; 
    } 
    [currentField becomeFirstResponder]; 
    prevField = currentField; 
} 
+0

皆さん、私はCocoaのベストプラクティスを教えていただきありがとうございますが、関連するコードだけを記述しようとしましたが、amountFieldは毎回インスタンス化されませんでしたが、私の質問には、私のテーブルは4行しかないので、スクロールしません。あなたのコメントの残りは非常に役立ちます。ありがとう! –

1

のように行うことができます

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"Cell"]; 
    [cell setSelectionStyle:UITableViewCellSelectionStyleBlue]; 

    amountField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 190, 30)]; 
    amountField.placeholder = @"Enter amount"; 
    amountField.keyboardType = UIKeyboardTypeDecimalPad; 
    amountField.textAlignment = UITextAlignmentRight; 
    amountField.clearButtonMode = UITextFieldViewModeNever; 

    [amountField setDelegate:self]; 
    [[cell textLabel] setText:@"Amount"]; 

    **for (UIView *cellSubViews in cell.subviews) { 
      cellSubViews.userInteractionEnabled = NO; 
    }** 

    [cell addSubview:amountField]; 
    return cell; 
} 

あなたは `userInteractionEnabled = NOを設定した場合、それは

関連する問題