0

UITextField を含む別のクラス (CustomCell) で定義されたカスタム プロトタイプ セルがある UITableView があります。ボタンを押すたびに、新しいセルを作成する addItem というメソッドが呼び出されます。UITextFields のテキストを配列に入れたい。よりよく説明するために、UITableView に 3 つのセルを追加し、対応する UITextFields に 3 つのテキストを入力すると、最初のセルのテキストはインデックス 0 の配列に移動し、2 番目のセルのテキストはインデックス 1 に移動します私の最大の問題は、セル 1 の UITextField に戻って更新し、それに対応する NSArray オブジェクトを動的に更新できるようにしたいということです。インデックス 0 に 1 つ。アプローチ方法がわかりません。誰か助けてくれませんか??? どうもありがとうございました!!

私のコード(obs:itemTableはUITableViewです):

MainViewController.m

@implementation addViewController{
    NSInteger n;
    NSString *aid;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)viewWillAppear:(BOOL)animated{
    n=1;
    aid=@"";
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return n;


}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *CellIdentifier= @"Cell";

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }

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

    return cell;

}
- (IBAction)addItem:(UIButton *)sender {
    ++n;
    [_itemTable reloadData];
}
- (IBAction)removeItem:(UIButton *)sender {
    if (n>=0)--n;
    [_itemTable reloadData];
}

CustomCell.m:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {_itemValue = [[UITextField alloc]init];

        _item = [[UILabel alloc]init];

        [self.contentView addSubview:_itemValue];

        [self.contentView addSubview:_item];

    }
    return self;
}

CustomCell.h

@interface CustomCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *itemNumber;
@property (strong, nonatomic) IBOutlet UITextField *itemValue;

@end
4

4 に答える 4

3

まず、各テキスト フィールドを作成するときに、そのテキスト フィールドのデリゲートを作成します。これにより、テキスト フィールドで何かが発生するたびにメッセージを受け取ることができます。

さて、ユーザーがテキスト フィールドに入力すると、メッセージが表示され、モデルを変更できます (配列、おそらく NSMutableArray として保持する必要があります)。しかし、そのためには、どのセルにこのメッセージの送信元のテキスト フィールドが含まれているかを把握する必要があります。次のようにします。

- (void)textFieldDidEndEditing:(UITextField *)tf {
    // some cell's text field has finished editing; which cell?
    UIView* v = tf;
    do {
        v = v.superview;
    } while (![v isKindOfClass: [UITableViewCell class]]);
    CustomCell* cell = (CustomCell*)v;
    // so which row is it?
    NSIndexPath* ip = [self.tableView indexPathForCell:cell];
    //  aha! so now ip.row is the row, and you can update your data model
    //   ... left as an exercise for the reader ...
}

私は自分の本のhttp://www.apeth.com/iOSBook/ch21.html#_editable_content_in_table_items (上記のコードの由来) でまさにこの種のことを行っているので、見て、どのようなアイデアが得られるかを見てください。 .

于 2013-04-03T01:52:42.953 に答える
0

明らかに、この問題にはいくつかの側面があります。まずUILabel、特定の UILabel がどの行にあるかを把握できるように、 への参照を復元できるようにする必要があります。次のtagように、プロパティを使用してこれを行うことをお勧めします。

_item = [[UILabel alloc] init];
_item.tag = 100; // or any value
[self.contentView addSubview:_item];

また、ラベル内のテキストが変更されるたびに呼び出されるアクションを設定する必要があります。次のように実行できます。

[_item addTarget:someObject
          action:@selector(labelChanged:)
forControlEvents:UIControlEventEditingChanged];

クラスsomeObjectが何であれ、次のシグネチャを持つメソッドが必要です。

- (void)labelChanged:(id)sender;

そのメソッド内で、それsenderが実際に aUILabelであることを確認できます。その後、新しいテキストに でアクセスできますsender.text

テキストを配置する配列内のポイントを把握するために、テーブル内の行数に対してループを宣言できます。

for (int i = 0; i < [mainViewControllerInstance tableView:theTableInQuestion numberOfRowsInSection:0]; ++i) {
    if(sender == [[mainViewControllerInstance tableView:theTableInQuestion
                                 cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]] viewWithTag:100]) {
        // Put `sender.text` in the appropriate spot in your array
    }
}

いくつかの追加メモ:NSMutableArray文字列を更新するので、文字列を追跡するために を使用しますが、ここにどのようなベスト プラクティスがあるかは完全にはわかりません。また、適切な行数を持つように配列を初期化することを確認し (配列を にするかどうかNSArray) 、ボタンが押されNSMutableArrayたときに配列に行を追加することを確認する必要があります。+まだ存在しない行のエントリを変更しようとすると例外が発生します。

于 2013-04-03T01:54:58.890 に答える