我在自定义单元格中有一个 UITextField
。一旦我在第一个和第二个单元格中输入值,并且当我滚动 UITableView
文本字段索引时,就会发生变化(第一个单元格值更改为最后一个单元格)。任何人都可以帮助我解决我的问题吗?
这是我的代码:
static NSString *cellIdentifier = @"TableCellID";
CustomTableViewCell *cell = (CustomTableViewCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[CustomTableViewCell alloc] init]; // or your custom initialization
}
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.txt_TCelltext.tag=indexPath.row;
cell.txt_TCelltext.placeholder=@"0.00";
[cell.txt_TCelltext setDelegate:self];
最佳答案
这是因为单元格重用。尝试从字典(或)数组填充 UITextField
数据。试试这个:
- (UITableViewCell *)tableView:(UITableView *)tableViewLocal
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Your Code
UITextField *textField = [[UITextField alloc]init];
textField.delegate = self;
textField.tag = indexPath.row;
if ([myTextFieldDictionary objectForKey:[NSString stringWithFormat:@"%ld", (long)textField.tag]])
{
textField.text = [[myTextFieldDictionary objectForKey:[NSString stringWithFormat:@"%ld", (long)textField.tag]];
}
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
[myTextFieldDictionary setValue:textField.text
forKey:[NSString stringWithFormat:@"%ld", (long)textField.tag]];
}
关于ios - 自定义单元格文本字段索引在滚动时发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29790352/