2016-01-21 68 views
-1

我已經創建了UITableViewUITableview每行都有一個以上UITextField。如何獲得每個UITextField值。以下是我的示例代碼。如何解決這個錯誤「這個類不是密鑰的關鍵值編碼」?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
    int j= 285;   
      for (int i=0; i<7; i++) { 
    NSString *txtIndvalue =[NSString stringWithFormat:@"Index%d",i];   
       UITextField *txtvalue = [[UITextField alloc] initWithFrame:CGRectMake(j,5.0f,25.0f,20.0f)]; 
       [txtvalue setBorderStyle:UITextBorderStyleLine]; 
    [txtvalue setTag:indexpath.row] 
    [txtvalue setvalue:txtIndvalue forkey:@"test"] 
       [cell addSubview:txtvalue]; 
       j = j + 35; 
      } 
    } 

error: Terminating app due to uncaught exception 'NSUnknownkeyException' ,reason :'[<UItextField .>' setValue:forUndefindekey:] this class is not key value codeing compliant for the key. Please help me. 

回答

2
[txtvalue setvalue:txtIndvalue forkey:@"test"] 

此行導致崩潰。 setValueForKey不是UITextField的財產/方法。如果你想設置文本框的文字,你應該使用

txt value.text = @"some text". 
+0

[txtvalue的setValue:txtIndvalue forkey:@ 「測試」 ]是UITextField的一個方法/屬性。 void setvalue:(id)forkey:(NSString *)。請你再檢查一次。如何爲每個文本框的每一行賦值。 – Baskar

+0

請檢查下面@BEN添加的答案。他正在使用KVC/KVO模式進行設置。 –

+0

任何示例代碼 – Baskar

0

,如果你想使用KVC爲UITextField試試這個:

[txtvalue setvalue:txtIndvalue forkey:@"text"]; 

編輯

試試這個:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
int j= 285;   
     for (int i=0; i<7; i++) { 
NSString *txtIndvalue =[NSString stringWithFormat:@"Index%d",i];   
      UITextField *txtvalue = [[UITextField alloc] initWithFrame:CGRectMake(j,5.0f,25.0f,20.0f)]; 
      [txtvalue setBorderStyle:UITextBorderStyleLine]; 
[txtvalue setTag:indexpath.row]; 
[txtvalue setvalue:txtIndvalue forkey:@"text"]; 
      [cell addSubview:txtvalue]; 
      j = j + 35; 
     } 
} 
+0

任何示例代碼。更新我的代碼。請幫幫我。 – Baskar

0

如果您使用的是KVC:

而不是使用[txtvalue setvalue:txtIndvalue forkey:@"text"];使用[txtIndvalue setValue:txtIndvalue forKeyPath:@"text"];

更新代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
int j= 285;   
     for (int i=0; i<7; i++) { 
NSString *txtIndvalue =[NSString stringWithFormat:@"Index%d",i];   
      UITextField *txtvalue = [[UITextField alloc] initWithFrame:CGRectMake(j,5.0f,25.0f,20.0f)]; 
      [txtvalue setBorderStyle:UITextBorderStyleLine]; 
[txtvalue setTag:indexpath.row]; 
[txtvalue setvalue:txtIndvalue forKeyPath:@"text"]; 
      [cell addSubview:txtvalue]; 
      j = j + 35; 
     } 
} 

打印KVC的價值:

NSLog(@"%@", [txtvalue valueForKey:@"text"]; 
+0

如何讓文本框輸入值。 – Baskar

+0

已更新的答案。 –

相關問題