gpt4 book ai didi

ios - 在 CellForRowAtIndexPath 中访问 UITextField

转载 作者:行者123 更新时间:2023-11-28 20:36:02 24 4
gpt4 key购买 nike

我在单元格上添加一个 UITextField。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}


myLoc = [[UITextField alloc] initWithFrame:CGRectMake(35, 10, 250, 40)];
myLoc.adjustsFontSizeToFitWidth = YES;
myLoc.textColor = [UIColor blackColor];
myLoc.textAlignment = UITextAlignmentCenter;
myLoc.placeholder = @"Enter Location";
myLoc.returnKeyType = UIReturnKeyDone;
myLoc.autocorrectionType = UITextAutocapitalizationTypeNone;
myLoc.tag = indexPath.row;
myLoc.delegate = self;
[myLoc setEnabled:YES];
[cell addSubview:myLoc];

return cell;
}

在 textFieldShouldReturn 中,我会将文本字段中的文本写入可变数组,并存储在 nsuserdefaults 中。

-(BOOL)textFieldShouldReturn:(UITextField *)textField{

[myLoc resignFirstResponder];

[locationArray addObject:textField.text];
locName = [NSUserDefaults standardUserDefaults];
[locName setObject:locationArray forKey:@"textName"];
[locName synchronize];

NSLog(@"Done pressed %@",myLoc.text);

return YES;

...但是 myLoc.text 始终为 null对我有什么想法吗?

最佳答案

在委托(delegate)方法中,不要使用myLoc 来引用textField,而是使用作为函数参数提供的实际textField 指针。这实际上应该指向正确的文本字段,因此 textField.text 应该具有正确的值。

旁注:您在哪里定义 myLoc?看起来您正在尝试在 View Controller 上设置一个属性。这样您将始终覆盖该属性。为此,您根本不需要属性,因此只需在函数范围内本地定义 myLoc,例如 UILabel *myLoc

关于ios - 在 CellForRowAtIndexPath 中访问 UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10487814/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com