質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Objective-C

Objective-Cはオブジェクト指向型のプログラミング言語のひとつです。C言語をベースにSmalltalkが取り入れられています。

Q&A

解決済

1回答

2300閲覧

Tableview上に配置したTextfieldへ値を入れる方法

teratai

総合スコア7

Objective-C

Objective-Cはオブジェクト指向型のプログラミング言語のひとつです。C言語をベースにSmalltalkが取り入れられています。

0グッド

0クリップ

投稿2016/06/07 02:14

###前提・実現したいこと
1、UIAlertControllerでYESをタップした時にTableview上に配置したTextfieldへ値を入れたいのですが実装方法が分かりません。(同時にdelegateにも値を保存させたいです。)
2、下記コードは2頁目なのですが、1頁目へ戻る方法もご教授いただけると幸いです。

###該当のソースコード

#import "CalcViewController.h"
#import "SettingViewController.h"
#import "CalcAppDelegate.h"
@implementation SettingViewController

@synthesize settinglist;
@synthesize ATextDelegate;
@synthesize BTextDelegate;
@synthesize CTextDelegate;

#pragma mark -
#pragma mark View lifecycle

  • (void)viewDidLoad {
    [super viewDidLoad];
    CalcAppDelegate calcAppDelegate;
    calcAppDelegate = (CalcAppDelegate
    )[[UIApplication sharedApplication] delegate];
    CalcView = calcAppDelegate.setting00;
    settinglist = [[NSMutableArray alloc] initWithObjects:
    @"AText",
    @"BText",
    @"CText",
    @"Change",
    nil];
    }

#pragma mark -
#pragma mark Table view data source

  • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;

}

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger count = [settinglist count];
    return count;

}

  • (CGFloat)tableView:(UITableView *)tableView
    heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80.0;
    }
  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    tableView.scrollEnabled = false;
    if (indexPath.row == 0) {
    UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(150.0f, 25.0f, 70.0f, 28.0f)];
    textField1.text = @"123";
    textField1.borderStyle = UITextBorderStyleRoundedRect;
    textField1.delegate = self;
    [cell.contentView addSubview:textField1];
    } else if (indexPath.row == 1) {
    UITextField *textField2 = [[UITextField alloc] initWithFrame:CGRectMake(150.0f, 25.0f, 70.0f, 28.0f)];
    textField2.text = @"456";
    textField2.borderStyle = UITextBorderStyleRoundedRect;
    textField2.delegate = self;
    [cell.contentView addSubview:textField2];
    } else if (indexPath.row == 2) {
    UITextField *textField3 = [[UITextField alloc] initWithFrame:CGRectMake(150.0f, 25.0f, 70.0f, 28.0f)];
    textField3.text = @"789";
    textField3.borderStyle = UITextBorderStyleRoundedRect;
    textField3.delegate = self;
    [cell.contentView addSubview:textField3];
    } else if (indexPath.row == 3) {
    UIButton *settingButton = [UIButton buttonWithType:UIButtonTypeCustom];
    settingButton.frame = CGRectMake(210.0f, 300.0f, 100.0f, 30.0f);
    [settingButton setTitle:@"Change", forState:UIControlStateNormal];
    [settingButton addTarget:self action:@selector(tapButton:) forControlEvents:UIControlEventTouchUpInside];
    [settingButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
    [self.view addSubview:settingButton];
    }
    }
    cell.textLabel.text = [settinglist objectAtIndex:indexPath.row];
    return cell;

nil];
}

-(void)tapButton:(id)sender {
NSLog(@"Riset Button tapped.");
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Check" message:@"Change?" preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self otherButtonPushed];
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"NO" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self cancelButtonPushed];
}]];
[self presentViewController:alertController animated:YES completion:nil];
}

  • (void)cancelButtonPushed { }

  • (void)otherButtonPushed {

    // ここで下記のようにしたいです。同時にDelegateにも代入させたいです。
    // textField1 = @"321";
    // textField2 = @"654";
    // textField3 = @"987";

}

###補足情報
Xcode Version7.3.1
初心者なので必要な情報が足りていないかもしれませんがご教授のほど宜しくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

はじめまして。

単純に解決したいのであれば、 cellForRowAtIndexPath 内で生成したテキストフィールドをクラス変数に持たせれば解決するような気がします。
(試してないのでうまく行かなかったらごめんなさい)

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 略 if (indexPath.row == 0) { self.textField1 = [[UITextField alloc] initWithFrame:CGRectMake(150.0f, 25.0f, 70.0f, 28.0f)]; self.textField1.text = @"123"; self.textField1.borderStyle = UITextBorderStyleRoundedRect; self.textField1.delegate = self; [cell.contentView addSubview:selftextField1]; } // 略 } (void)otherButtonPushed { // self. で参照出来る self.textField1.text = @"321"; self.textField2.text = @"654"; self.textField3.text = @"987"; }

他にも、otherButtonPushed内でUITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];を叩き、指定行のセルを取得。
その後、取り出したセルのsubViewの中からtextViewを取り出して設定する方法とかもありますが。


但し、そもそも論ですが cellForRowAtIndexPath 内で毎回テキストフィールドを生成し、毎度addSubviewをするのはあまりおすすめ出来ません。
何故なら、TableViewはセルを使い回す動きをするので、多重にaddSubviewされてしまう危険性があるからです。

参考: UITableViewのセルの使いまわしについて
http://blog.morizotter.com/2013/12/24/uitableview-customcell-storyboard/

例えば、セルを再描画しようと思って[tableView reloadData];とすると、既にtextFieldが追加されているセルにさらに追加しようとする動きをしてしまいます。
一度、構成を見直すことをおすすめします。

投稿2016/06/09 04:24

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

teratai

2016/06/13 00:40

ありがとうございます。 今一度見直しをしてみようかと思います。 selfもイマイチ理解できていないところがあり、もう少し勉強をしてみたいと思います。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問