我有一个UITableView,它有一个定制的UITableViewCell,里面有一个UITextField。
每个UITextField都显示来自viewModel的一些文本,我使用Reactive-Cocoa将文本字段绑定到viewModel。
当我的UITableView第一次加载时,一切正常。但是,当我为下一个‘页面’重新加载UiTableView时--重新加载的第一个UiTextField (第二页)-- tableView具有与第一个‘页面’中的第一个UITextField完全相同的内存地址--单元格与其他UI元素不一样--只是文本字段是相同的实例。
因此,我在VC中声明了UITextField,如下所示:
@property (weak, nonatomic) UITextField *textFieldOne; //One the first 'page'
@property (weak, nonatomic) UITextField *textFieldTwo; //After reload on second 'page' 然后像这样在cellForRowAtIndexPath调用的方法中安装
-(void)configureTextFieldCell:(BBTextFieldLabelTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
cell.textField.delegate = self;
if (self.selectedSegmentIndex == SegmentedControlStep1){
if (indexPath.section == 0){
cell.label.text = @"Name";
self.textFieldOne = cell.textField;
}
/* Code for setting up other cells / textfields ommited -
but the same syntax as above with checks for indexPath */
}
if (self.selectedSegmentIndex == SegmentedControlStep2){
cell.label.text = @"Username";
self.textFieldTwo = cell.textField;
[self bindUsernameAndPasswordToViewModel]; /* Binding for this textfield as its nil when VC loads for the first time,
so this is the first chance I get to bind it on second page */
}
}在BBTextFieldLabelTableViewCell中,UITextField声明如下:
@property (strong, nonatomic) IBOutlet UITextField *textField;我还尝试在单元格的实现文件中这样做:
-(void)prepareForReuse
{
self.textField = [[UITextField alloc] init];
}正如我所想的,我的问题可能是某种类型的细胞重用问题。然而,这段代码并没有什么区别。
所以textFieldOne和textFieldTwo都有完全相同的内存地址,我不知道为什么。
在cellForRowAtIndexPath内部,我创建这样的单元格:
BBTextFieldLabelTableViewCell *textFieldCell = [tableView dequeueReusableCellWithIdentifier:textFieldCellidentifier];发布于 2015-07-21 08:13:28
在您的prepareForReuse中,您正在创建一个新的文本字段,但是您既不删除旧的文本字段,也不添加新的文本字段。
我建议使用prepareForReuse重置当前文本字段,而不是创建新字段。
更仔细地阅读您的问题: textfield 1和textfield都具有相同的值,这表明对configureTextFieldCell的两个调用之间并没有调用用于重用的准备。如果没有更多的代码,就很难理解为什么
https://stackoverflow.com/questions/31532231
复制相似问题