2011-12-20 11 views
0

他の多くのユーザと同様に、UITextFieldをUITableViewCellにスローして行を編集するように設定しようとしています。しかし、新しいビューを追加する代わりに、私はaccessoryViewプロパティを使用しようとしています。最初に私は...UITextFieldがアクセサリビューでUITableViewCellに追加された場合、最初のレスポンダになることができない

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     UITextField *textField =[[UITextField alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 44.0f)]; 
     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
     cell.accessoryView = textField; 
     [textField becomeFirstResponder]; 
} 

これを試してみました...で、UITextFieldが正しく追加されますが、私はキーボードを表示するために取得するためにそれを再度タップする必要があります。私はそれが動作します...

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     UITextField *textField =[[UITextField alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 200.0f, 44.0f)]; 
     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
     [cell addSubview:textField]; 
     [textField becomeFirstResponder]; 
} 

...これを行う場合は、キーボードが現れますが、私はaccessoryViewとしてそれを持つことの他の利点のいずれか(移動する他のビューで簡単にポジショニング得ることはありませんその周りに)。私はaddSubview:呼び出しがレスポンダーチェーンや何かを変更して、私のUITextFieldを最初のレスポンダーにすることを許可していると思いますが、これを行うための別の方法が考えられました。ありがとう。

答えて

0

私の解決策は嫌いですが、うまく機能しています。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];    

    CGRect detailTextLabelFrame = cell.detailTextLabel.frame; 
    detailTextLabelFrame.origin.x -= 100.0f; 
    detailTextLabelFrame.size.width += 100.0f; 

    UITextField *textField = [[UITextField alloc] initWithFrame:detailTextLabelFrame]; 

    cell.detailTextLabel.hidden = YES; 
    cell.accessoryView = textField; 

    // These are the two lines that made it work, but it feels ugly  
    [cell.accessoryView removeFromSuperview]; 
    [cell addSubview:cell.accessoryView]; 

    [cell.accessoryView becomeFirstResponder]; 
} 
0

私は、あなたがテキストフィールドの代理人について知りたがっていると思います。このようにして....あなたに役立つかもしれません。

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField { 

if(theTextField == yourtextfieldname) { 
    [yourtextfieldname resignFirstResponder]; 
} 

return YES; 
} 
+0

こんにちはEmon、私は実際にキーボードを表示することもできませんでした。しかし、はい、私はキーボードを却下するために同様のものを使用しています。 – rob5408

関連する問題