在 iOS 中更改本机下拉菜单时,动态或以编程方式创建的文本框值会外生

Dynamically or programatically created text box values occurs extraneously on changing native drop downs in iOS

我创建了本机下拉菜单 (UIButton+UITableview) 和文本字段 programatically.Inside 方法 cellForRowAtIndexPath。

如果我更改了下拉值,则文本框的值已经在其他文本框上进行了无关的设置。

图片 1)

图 2)

1.First Image:In 我输入文本框的第一个图像当时的值是 1,1,1 manually.During,第一个下拉菜单具有相同的值。

2.Second Image:In 第二张图片如果我更改下拉菜单 values.Extraneous 文本框值已经生成。(比较第一张和第二张图片)。

所以,如何纠正这个错误。

我的 cellForRowAtIndexPath 代码:

- (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];
}
if (tableView == self.tblview)
{
//Buttons created programatically
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn1.frame = CGRectMake(0.0f, 5.0f, 75.0f, 30.0f);
[btn1setTitle:projectDD forState:UIControlStateNormal];
[cell addSubview:btn1];
[btn1 addTarget:selfaction:@selector(proDropDwon:)
forControlEvents:UIControlEventTouchUpInside];
[btn1 setBackgroundImage:bgImage forState:UIControlStateNormal];
[btn1 setTag:indexPath.row];
//Text field or text box created programatically.
UITextField *text1 =  [[UITextField alloc] initWithFrame:CGRectMake(225.0f,5.0f,50.0f,20.0f)];
[text1 setBorderStyle:UITextBorderStyleLine];
[cell addSubview:text1];
[text1 setTag:indexPath.row];
[text1 addTarget:self action:@selector(text1Change:) forControlEvents:UIControlEventEditingChanged];
[text1 setText:text1];
int  j= 100;
for (int i=0; i<7; i++) {
UITextField *text2 =  [[UITextField alloc] init];
text2.frame = CGRectMake(j,5.0f,25.0f,20.0f);
[text2 setBorderStyle:UITextBorderStyleLine];
[cell addSubview:text2];
[text2 setTag:indexPath.row];
[text2 setAccessibilityLabel:txtIndValue];
[text2 addTarget:self action:@selector(text2Change:) forControlEvents:UIControlEventEditingChanged];
[text2 setKeyboardType:UIKeyboardTypeNumberPad];
j = j + 35;
}
return cell;
}

同样,我为其他 2 个下拉菜单创建了 btn2 和 btn3。

我的 didSelectRowAtIndexPath:I 的代码因为 reloading.So 的思想问题,我把有关表格视图的代码。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tblview beginUpdates];
[self.tblview reloadRowsAtIndexPaths:[self.tblview   indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tblview endUpdates];
[self.tblview reloadData];
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}

btn1 的代码被点击:

- (IBAction)proDropDown:(id)sender
{
UIButton *btn1 = (UIButton *) sender;
self.projIndexPath = [NSString stringWithFormat:@"%ld", btn1.tag];
if (self.tblviewProject.hidden ==YES) {
self.tblview1.hidden = NO;
[self.view addSubview:self.tblview1];
}
else {
self.tblview1.hidden = YES;
}
}

到现在为止,我尝试在单击的 btn1 中重新加载 tableview,评论重新加载 data.where 我必须在 cellForRowAtIndexPath 或 didSelectRowAtIndexPath.or 中设置值中进行代码更改 button.Please 建议我 ideas.thanks提前。

您的方法存在多个问题。

  1. 重复使用单元格时,您每次都以编程方式创建按钮并将其添加到上下文视图中。因此,您最终会在单个 view.Instrument 代码上积累大量控件,您应该会看到内存在增加。 为了解决这个问题,您只能在创建新单元格时添加控件。假设你只有一种类型的细胞,这会很好。

  2. 由于每个单元格都有固定的控件,因此最好设计一个静态自定义单元格,这将使您的工作不那么复杂。

  3. 按钮点击代码 "proDropDwon"/"proDropDwon" 不匹配。这会导致运行时异常。

  4. 这种说法很奇怪,这很可能是您遇到问题的原因。 [text1 setText:text1]; 作为文本字段的 text1 无法分配给文本 属性。

with 3 and 4 您发布的代码似乎不是您遇到问题的实际代码。能给出相关代码吗?