2

私は今、何日UITextFieldsもセクションのセルの入力を設定して取得しようとしています。

私の最初のビューUITextFieldでは、必要なセクションの数を指定する場所に移動し、ボタンを押すと、そのセクション (4 行) がUITableView. 私の問題は、 each の正しい値を取得できないことですUITextField

例えば:

  • すべてのセクションに 4 つのテキストフィールドがあります
  • のテキストフィールドに int または float を書き込むことができます
  • テキストフィールド 1 と 2 の int がわかったら、それらを分割します...

ここに私のコードがあります....

#import "ViewController.h"

@interface ViewController () 
{
    NSMutableArray *sectionsArray;
    NSMutableArray *rowsArray;
    NSMutableArray *textFieldArray;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.

    [[self myTableView]setDelegate:self];
    [[self myTableView]setDataSource:self];

    sectionsArray = [[NSMutableArray alloc]init];
    rowsArray = [[NSMutableArray alloc]initWithObjects:@"",@"",@"",@"", nil];
    textFieldArray = [[NSMutableArray alloc]initWithObjects:@"",@"",@"",@"", nil];

}

- (void)viewWillAppear:(BOOL)animated 
{
    [super viewWillAppear:animated];
    [self.myTableView reloadData]; 
}


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

#pragma mark - Table View

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

    int anzahl = [_AnzahlStationen intValue];//from first view...anzahl=quantity of sections

    //adds sections
    do {

        [sectionsArray addObject:[NSString stringWithFormat:@"Palette %lu",sectionsArray.count+1]];

    } while ([sectionsArray count] < anzahl);

    return sectionsArray.count;
}


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

    return rowsArray.count;
}


-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{
    return  [sectionsArray objectAtIndex:section];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell==nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 300, 24)];
        textField.font = [UIFont fontWithName:@"Gujarati Sangam MN" size:15];
        textField.textColor = [UIColor redColor];

        textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        textField.tag = 17;
        [cell.contentView addSubview:textField];
        textField.delegate = self;
    }

    UITextField *textField = (UITextField *)[cell.contentView viewWithTag:17];

    textField.text = [textFieldArray objectAtIndex:indexPath.row];

    return cell; 
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES; 
}

- (void)textFieldDidEndEditing:(UITextField *)textField {

    [textFieldArray replaceObjectAtIndex:0 withObject:textField.text];
}

@end

正しい UITextField を正しい行に置き換えるにはどうすればよいですか?

4

1 に答える 1

1

次のようなデータソースを使用することをお勧めします。

NSMutableArray *tableViewData = [NSMutableArray array];

[tableViewData addObject:@[ @{@"textfield" : YourTextField} ]];
[tableViewData addObject:@[ @{@"textfield" : YourTextField} ]];
[tableViewData addObject:@[ @{@"textfield" : YourTextField} ]];
[tableViewData addObject:@[ @{@"textfield" : YourTextField} ]];

上記のコードはtableViewData、4 つの配列を含む を表し、各配列には、1 つのセルのデータを保持する 1 つのディクショナリが含まれます。私の例では、UITextField.

これは好きなように動的に作成でき、デリゲート メソッドとデータソース メソッドはそこから読み取ることができます。3 つの個別の配列を持つのではなく。

例えば

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
     return [tableViewData count];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[tableViewData objectAtIndex:section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    NSDictionary *cellData = [[tableViewData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

    // Create your cell and use the data stored in the dictionary
}

上記のアプローチを使用することで、この辞書を更新し、すべての状態をUITableView1 か所で維持することができ、コードを整理して読みやすく保つことができます。任意の にアクセスするには、データソースおよびデリゲート メソッドUITextFieldのディクショナリで検索するだけです。UITableView

お役に立てれば!

于 2013-10-24T09:16:11.577 に答える