我有一個UICollectionView
加載我的自定義UICollectionViewCell
的有UITextField
他們和UILabel
。ReactiveCocoa連接到UICollectionViewCell子視圖
在我cellForItemAtIndexPath
我做這樣的事情:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
BBCollectionViewTextFieldCell *textFieldCell = [collectionView dequeueReusableCellWithReuseIdentifier:[BBCollectionViewTextFieldCell reuseIdentifier] forIndexPath:indexPath];
NSString *label = @"";
switch (indexPath.item){
case 0:
label = @"Label One";
self.firstTextField = textFieldCell.textField;
textFieldCell.textField.text = self.viewModel.labelOneData;
break;
case 1:
label = @"Label Two";
self.secondTextField = textFieldCell.textField;
textFieldCell.textField.text = self.viewModel.labelTwoData;
break;
case 2:
label = @"Label Three";
self.thirdTextField = textFieldCell.textField;
textFieldCell.textField.text = self.viewModel.labelThreeData;
break;
}
textFieldCell.label.text = label;
textFieldCell.textField.delegate = self;
return textFieldCell;
}
然後我用正常UITextFieldDelegate
方法來處理文本輸入做這樣的事情:
-(void)textFieldDidEndEditing:(UITextField *)textField{
if (textField == self.firstTextField){
//Do something with it
}
//And so on for the rest...
}
到目前爲止好和所有作品...
那麼是什麼問題?
的問題是,如果我重新加載UICollectionView
會發生以下情況:
self.firstTextField
將在它的數據屬於self.thirdTextField
或者任何隨機組合。 UILabel
都是正確的 - 但看起來實際上UItextField
已混合起來。第一個UICollectionViewCell
的UitextField
實際上將來自另一個單元格的textField的數據。
起初我認爲這是一個重用問題 - 但是,因爲我的單元格永遠不會從屏幕上滾動並且數據非常靜態(總是有X個單元格,不會少於或者更多) - 所以它不能成爲重用問題。
我修改了代碼,使我在我的UIViewController
沒有UitextField
Properties
其中該代碼位於 - 並依靠indexPath
得到文本框。這樣在cellForItemAtIndexPath
switch (indexPath.item){
case 0:
label = @"Label One";
textFieldCell.textField.text = self.viewModel.labeloneData;
break;
和:
-(void)textFieldDidEndEditing:(UITextField *)textField{
NSIndexPath *indexPath = [self.collectionView indexPathForCellContaininView:textField];
if(indexPath.item == 0){
//Do something with it
}
//And so on for the rest...
}
然而,這解決了問題,這不是我想做的事情。我需要的UItextField
性質在我UIViewController
我宣佈在像這樣實施UitextField
Properties
:
@property (strong, nonatomic) UITextField *firsttextField;
我也貼一個非常類似的問題,但使用UITableView
,而不是和它結束了對電池再利用相關 - 但是我不相信這是了(同樣,設置程序的問題的代碼幾乎是相同的,這個問題 - 同樣的問題,雖然)
UITextField in UITableViewCell - reuse issue
當問題是我覺得
我不認爲這是與UITableView
或UiCollectionView
被重用細胞的方式做。我認爲這個問題在viewController's
代碼的某處和我的UItextField
屬性的實例..我雖然可能不在這裏。
我知道我列出一個可能的解決方法 - 不過,我希望得到這個問題的底部,找到如何使用它的原因。和潛在的加擾的重新加載 -
做你嘗試給標籤文本框? –
我想過 - 我會在cellForRowAtIndexPath方法中設置標籤嗎? – Tander
是....嘗試cellforrowatindex –