我有一个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 –