ios - 自定义单元格文本字段索引在滚动时发生变化

标签 ios objective-c xcode uitableview uitextfield

我在自定义单元格中有一个 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];

enter image description here

最佳答案

这是因为单元格重用。尝试从字典(或)数组填充 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/

相关文章:

ios - 找不到 Metal iOS 的资源

ios - 从 Objective C 中的文件中读取字节数组

objective-c - ARC - 为什么对象指针在函数定义中需要明确的所有权类型?

ios - 将两个 UIImage 合并为 1 以保存到库

ios - 使用 Graph API 或 FQL 将所有照片包含在单个 Facebook 提要帖子中?

iphone - NSMutableArray 中的元素数

c++ - 来自 Xcode 8.3.2 : Non-portable path to file - specified path differs in case from file name on disk 的错误警告

objective-c - 在 iphone 中全局访问整数和字符串?

iPhone 5 的弱光增强模式

ios - 我可以使用 NSUserDefaults 保存应用程序的表格单元格值吗?