从文本字段获取值并将其存储在Objective-C中的字典中

时间:2019-01-06 06:57:00

标签: objective-c uitableview for-loop uitextfield nsdictionary

我在textfield中有一个tableviewcell。在按钮上单击我想将文本字段中的值存储在字典中。 但是,虽然其中有值,但其中一个文本字段仍返回nil。

NSString *sEmpty= [[NSString alloc] init];

for(int i = 0; i < 13; i++){

  myProfileCell *cell = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];

  sEmpty = cell.tfProfileDetails.text;

  if (sEmpty == nil) {
   sEmpty = @"";
  }

  NSString *str;

  switch (i) {
   case 0:
    str = @"first_name";
    [_userDict setObject:sEmpty forKey:str];
    sEmpty = nil;

    break;

   case 1:
    str = @"middle_name";
    [_userDict setObject:sEmpty forKey:str];
    sEmpty = nil;

    break;

   case 2:
    str = @"last_name";
    [_userDict setObject:sEmpty forKey:str];
    sEmpty = nil;

    break;

   case 3:
    str = @"profile";
    [_userDict setObject:sEmpty forKey:str];
    sEmpty = nil;

    break;

   case 4:
    str = @"email";
    [_userDict setObject:sEmpty forKey:str];
    sEmpty = nil;

    break;

   case 5:
    str = @"phone";
    [_userDict setObject:sEmpty forKey:str];
    sEmpty = nil;

    break;

   case 6:
    str = @"birth_date";
    [_userDict setObject:sEmpty forKey:str];
    sEmpty = nil;

    break;

   case 7:
    str = @"sex";
    [_userDict setObject:sEmpty forKey:str];
    sEmpty = nil;

    break;

   case 8:
    str = @"city";
    [_userDict setObject:sEmpty forKey:str];
    sEmpty = nil;

    break;

   case 9:
    str = @"bio";
    [_userDict setObject:sEmpty forKey:str];
    sEmpty = nil;

    break;

   case 10:
    str = @"address";
    [_userDict setObject:sEmpty forKey:str];
    sEmpty = nil;

    break;

   case 11:
    str = @"pincode";
    [_userDict setObject:sEmpty forKey:str];
    sEmpty = nil;

    break;

   case 12:
    str = @"bank_acc_type";
    [_userDict setObject:sEmpty forKey:str];
    sEmpty = nil;

    break;

   default:
    break;
  }


}


NSLog(@ "%@ ",_userDict);

当代码进入案例11(密码)时-文本字段将零值存储在 sEmpty 中,但是我在模拟器的所有文本字段中输入了值。 请建议我一些可能的结果,以便所有值都存储在字典中。

P.S-如果文本字段中的值为空,则字典中的值将存储为空字符串

1 个答案:

答案 0 :(得分:1)

根据苹果公司cellForRowAtIndexPath:的文档。此方法将返回

  

代表表单元格的对象,如果该单元格不可见或indexPath超出范围,则为nil。

我猜想,表格视图显示11个单元格,<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <div class="container"> <div class="row"> <div class="col-xs-6 col-sm-6"> <h2>Youtube</h2> <div class="embed-responsive embed-responsive-16by9"> <iframe class="embed-responsive-item" src="https://www.youtube.com/embed/RBumgq5yVrA?autoplay=0&start=0&rel=0&modestbranding=1&showinfo=0&iv_load_policy=3&disablekb=1&playsinline=1&cc_lang_pref=auto&enablejsapi=1"></iframe> </div> </div> <div class="col-xs-6 col-sm-6"> <h2>Vimeo</h2> <div class="embed-responsive embed-responsive-16by9"> <iframe class="embed-responsive-item" src="https://player.vimeo.com/video/304704403?autoplay=0&api=1"></iframe> </div> </div> </div> </div>是第12个单元格,在屏幕上不可见。这使得pincode在此索引路径的单元格返回cellForRowAtIndexPath:,导致文本字段,而nilsEmpty

要解决此问题,我建议将字符串保留在数组中。每当用户在文本字段上键入内容时,都要在该索引处更新字符串值。当您需要输入字符串时,请从数组而不是单元格获取