[英]How to get value of uiTextField created programmatically in a uiTableView?
[英]UITextfield Value in UITableView
我在UITableviewController
內部有一個文本UITableviewController
。 我想在用戶完成鍵入並移至下一個字段后讓用戶輸入數據。 我想存儲這些信息。
因為一旦我滾動用戶輸入的數據就會消失。
任何幫助表示贊賞。
if (([indexPath section] == 1) && ([indexPath row] == 2))
{
static NSString *CellIdentifier1 = @"Cell";
GeneralInfocell *cell = (GeneralInfocell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
[[NSBundle mainBundle] loadNibNamed:@"GeneralInfocell" owner:self options:nil];
cell = addresscell;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
addresscell.txtStreet.delegate = self;
addresscell.txtStreet.font = [UIFont fontWithName:@"Arial" size:14];
addresscell.txtStreet.keyboardType = UIKeyboardTypeDefault;
addresscell.txtStreet.returnKeyType = UIReturnKeyNext;
addresscell.txtStreet.backgroundColor = [UIColor whiteColor];
addresscell.txtStreet.autocorrectionType = UITextAutocorrectionTypeNo;
addresscell. txtStreet.autocapitalizationType = UITextAutocapitalizationTypeNone;
addresscell. txtStreet.textAlignment = UITextAlignmentLeft;
addresscell.txtStreet.textColor = [UIColor blackColor];
[addresscell.txtStreet addTarget:self action:@selector(textFieldDidEndEditing:) forControlEvents:UIControlEventEditingDidEnd];
}
在我的textFieldDidEndEditing
方法中,
- (void)textFieldDidEndEditing:(UITextField *)textField {
UILabel *label = [NSString stringWithFormat:@"%@",[addresscell.txtCity text]];
NSLog(@"label:%@",label);
}
但是我沒有任何價值。
謝謝
您的UITextFieldDelegate
應該是這樣的–
- (void)textFieldDidEndEditing:(UITextField *)textField {
NSLog(@"Data entered: %@", textField.text);
}
由於其他所有接線均正確,因此您現在應該獲得適當的值。
由於單元已被重用,因此滾動時會丟失數據。 每次請求單元格時,您都需要重置文本字段的值。 為此,如果有多個字段(每個文本字段一個),則可以維護可變的字符串數組。 取消文本字段后,保存在數組中輸入的數據,並在下次請求該單元格時將其還原。
讓我知道您是否需要任何進一步的幫助。
編輯
tag
所有10個文本字段0到9。
在視圖控制器中聲明一個NSMutableArray iVar,例如data
,以存儲文本字段的字符串。
在-viewDidLoad:
,
...
data = [[NSMutableArray] initWithCapacity:10];
for ( int i = 0; i < 10; i++ ) {
[data addObject:@""];
}
...
在-textFieldDidEndEditing:
,
...
[data replaceObjectAtIndex:[textField tag] withObject:[textField text]];
...
在-tableView:cellForRowAtIndexPath:
,
...
// Since they are named variables, it will be something on lines of
addressCell.txtStreet.text = [data objectAtIndex:[addressCell.txtStreet tag]];
// Repeat for all other text fields
...
不要忘記在dealloc
方法中釋放data
。
實現UITextFieldDelegate
協議和textFieldDidEndEditing:
方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.