-1

UIView Controllerテキストフィールドからセル内のテーブルビューラベルにtextFieldデータを渡すにはどうすればよいですか? 配列カウントに値を返す行数を設定できるように、何らかの方法でテキストフィールドデータを配列に追加したいと思います。

モデルに NSMutable 配列を追加しました。

私のView Controllerでは、prepareForSegueメソッドを実装しています

  if ([segue.identifier isEqualToString:@"Details"]){

        MileageDetailViewController * mdv = [segue destinationViewController];

        NSString *text;
        startTextField.text = text;
        mdv.label.text = text;

私はこれをいくつかの異なる方法で試しました。私はこれを配列で行い、テキストオブジェクトを配列に追加しようとしましたが、どちらも表示されませんでした。この最後の方法で、ラベルを使用して、textField からテーブルビュー ラベルにテキストを追加しようとしました。

 In the tableView I add this code to grab the text from the viewController.

     - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    { 
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return  1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath      *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Reuse"];

    UILabel *labe = (UILabel *) [cell viewWithTag:20];

    labe.text = label.text;

    // Configure the cell...

    return cell;
4

2 に答える 2

1

MVC 設計パターンに慣れる必要があります。配列を保持するモデル クラスを作成します。テキスト フィールドが変更されたら、配列を更新します。テーブルビューのView Controllerが同じモデルオブジェクトを監視している場合(おそらくKVOを使用)、配列が変更されたときに自動更新できます。

于 2013-01-22T23:49:43.610 に答える
0

以下のコード例は、開始するためのものです。しかし、iOS 開発を始めたばかりのように見えるので、Ray Wenderlich のサイト (リンク) のチュートリアルに従うことを強く強くお勧めします。

以下を使用してView Controller間でデータを渡すprepareForSegue:(コードブロックにコメントあり):

// This will get called before the view appears
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"Details"]) {
        // Get destination view controller
        MileageDetailViewController *vc = [segue destinationViewController];

        // Grab the text field contents and put it into a public property on the new view controller
        vc.myText = self.myTextField.text;
        // NOTE: myText is a public NSString property on your MileageDetailViewController
        // NOTE: self.myTextField is the IBOutlet connected to your Text Field
    }
}

次に、テーブル ビューを使用してコントローラーのメソッドに進みます。

// Load the model required for the view controller
- (void)viewDidLoad {
    [super viewDidLoad];

    // Load your "model" - in this case an array property (called myData) with a single piece of text
    // NOTE: myText CANNOT be nil or an exception will occur
    self.myData = [[NSArray alloc] initWithObjects: self.myText, nil];
}


// UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{ 
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return  [self.myData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath      *)indexPath
{
    // NOTE: You must have set the reuse identifier in Interface Builder for this to work
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Reuse"];

    // NOTE: You must have set the tag in Interface Builder for this to work
    UILabel *labe = (UILabel *)[cell viewWithTag:20];

    labe.text = [self.myData objectAtIndex:indexPath.row];

    return cell;
}

お役に立てれば; iOS 開発はとても楽しいものですが、いくつかのチュートリアルに従うことで、すぐに使いこなせるようになります。

于 2013-01-23T16:36:04.253 に答える