【发布时间】:2015-07-21 06:55:52
【问题描述】:
我有一个UITableView,它有一个自定义UITableViewCell,里面有一个UITextField。
每个UITextField 都显示来自我的viewModel 的一些文本,我正在使用Reactive-Cocoa 将文本字段绑定到viewModel。
当我的UITableView 第一次加载时,一切正常。但是,当我为下一个“页面”重新加载UiTableView - 重新加载时的第一个UiTextField(第二页)tableView 与第一个“页面”中的第一个UITextField 具有完全相同的内存地址 - 单元格不是与其他 UI 元素相同是正确的 - 只是文本字段是相同的实例。
所以,我在我的 VC 中声明了 UITextFields,如下所示:
@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];
【问题讨论】:
-
你检查过cell的内存地址吗?
-
有趣...地址相同,但在某些情况下,UI 元素不正确。似乎 UITableView 正在重用确切的单元格并且没有正确更新内容。知道如何解决这个问题吗?
-
如果目的和 UI 不同,您可以为第二个屏幕制作不同的自定义单元格。
-
@AnushaK UI 是相同的,除了单元格中的 UILabel。有时标签设置不正确(与第一页上的值相同) - 似乎 UITableView 使用的是完全相同的单元格。
标签: objective-c uitableview uitextfield