6

私は現在、これを機能させようとしています。どんな助けでも大歓迎です。

私には内なる習慣UITableViewCellがありUITextFieldます。UITextField表のセルを選択したら、最初のレスポンダーを作成したいと思います。
ただし 、 false[textField becomeFirstResponder];を返します。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
  UITextField *textField;
  for (UIView *view in [cell subviews]) {
    if ([view isMemberOfClass:[UITextField class]]) {
      textField = ((UITextField *)view);
    }
  }

  [textField becomeFirstResponder];
}

要求どおり、テキストフィールドの初期化。これはUITableViewCellサブクラスで行われます。

- (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

  if (self) {
    // Init Textfield
    _textField = [[UITextField alloc] init];
    _textField.backgroundColor = [UIColor clearColor];
    _textField.delegate = self;
    [self addSubview:_textField];
  }

return self;
}
4

8 に答える 8

16

カスタム セルがある場合は、次のようなことができます。

@implementation CustomCell

#pragma mark - UIResponder

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

- (BOOL)becomeFirstResponder
{
    return [self.textField becomeFirstResponder];
}

@end

次に、テーブル ビュー デリゲート内で次のようにします。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if ([cell canBecomeFirstResponder]) {
        [cell becomeFirstResponder];
    }
}
于 2014-08-13T01:50:05.373 に答える
3

tagそれぞれに与えてUITextField、次のコードを使用します。

UITableViewCell* cell = (UITableViewCell*)sender.superview.superview;
UITextField *txtField = (UITextField*)[cell viewWithTag:tag];
[txtField becomeFirstResponder];

didSelectRowAtIndexPathメソッドで。

非常にうまく機能しています.. :)

于 2013-04-04T10:49:02.993 に答える
2

textFielddelegateプロパティを設定する必要があると思います。

呼び出す前に、メソッドに追加textField.delegate = self;します[textField becomeFirstResponder]

于 2013-04-04T11:09:26.933 に答える
2

セルを作成するときに、テキストフィールドにタグ値を含める必要があると思います:

- (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
      self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

      if (self) {
        // Init Textfield
        _textField = [[UITextField alloc] init];
        _textField.backgroundColor = [UIColor clearColor];
        _textField.delegate = self;
        _textField.tag = 999;
        [self addSubview:_textField];
      }

    return self;
}

そして didSelectRowAtIndexPath メソッドで、タグ値を使用してオブジェクトを復元します。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
  UITextField *textField = (UITextField*)[cell viewWithTag:999];


  if (![textField isFirstResponder]){
     [textField becomeFirstResponder];
  }

}

テキストフィールドがすでに最初の応答者であるかどうかを制御するための検証を含めました。

期待どおりに動作することを願っています

于 2013-05-02T14:41:12.430 に答える
0
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
  UITextField *textField;
  for (id subV in [cell subviews]) {
    if ([subV isKindOfClass:[UITextField class]]) {
      textField = (UITextField *)subV;
      [textField becomeFirstResponder];
      break;
    }
  }


}

お役に立てると思います。

于 2013-04-04T11:01:33.700 に答える
0

@josh-fuggle からの回答を Swift 2.2 を使用するように更新しました。

import Foundation
import UIKit

class CustomTableCell:UITableViewCell {

    @IBOutlet var textValue: UITextField!

    override func canBecomeFirstResponder() -> Bool {
        return true
    }

    override func becomeFirstResponder() -> Bool {
        return self.textValue.becomeFirstResponder()
    }
}

そして、これはあなたのテーブルビューデリゲートにあります:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        let cell = tableView.cellForRowAtIndexPath(indexPath)
        if ((cell?.canBecomeFirstResponder()) != nil) {
            cell?.becomeFirstResponder()
        }
}
于 2016-06-10T16:03:59.523 に答える