我有一個UITableView
,它有一個自定義UITableViewCell
,其中有一個UITextField
。UITableViewCell中的UITextField - 重用問題
每個UITextField
顯示一些文本從我的viewModel
和我使用Reactive-Cocoa
將文本字段綁定到viewModel
。
當我的UITableView
第一次加載時,一切正常。但是,當我爲下一個'頁面'重新加載UiTableView
- 第一個UiTextField
重新加載(頁面2)時,tableView與第一個'頁面'中的第一個UITextField
具有完全相同的內存地址 - 單元格與其他UI元素不同正確 - 只是文本字段是相同的實例。
因此,我宣佈UITextField
在我的VC像這樣:
@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];
你檢查了單元的內存地址嗎? –
有趣的...地址是相同的,在某些情況下,UI元素是不正確的。看起來好像UITableView重複使用確切的單元格,而不是正確地更新內容。任何想法如何我可以解決這個問題? – Tander
如果目的和用戶界面不同,您可以爲第二個屏幕創建不同的自定義單元格。 –