2015-08-17 94 views
1

我有一个UICollectionView加载我的自定义UICollectionViewCell的有UITextField他们和UILabelReactiveCocoa连接到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已混合起来。第一个UICollectionViewCellUitextField实际上将来自另一个单元格的textField的数据。

起初我认为这是一个重用问题 - 但是,因为我的单元格永远不会从屏幕上滚动并且数据非常静态(总是有X个单元格,不会少于或者更多) - 所以它不能成为重用问题。

我修改了代码,使我在我的UIViewController没有UitextFieldProperties其中该代码位于 - 并依靠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

我宣布在像这样实施UitextFieldProperties

@property (strong, nonatomic) UITextField *firsttextField; 

我也贴一个非常类似的问题,但使用UITableView,而不是和它结束了对电池再利用相关 - 但是我不相信这是了(同样,设置程序的问题的代码几乎是相同的,这个问题 - 同样的问题,虽然)

UITextField in UITableViewCell - reuse issue

当问题是我觉得

我不认为这是与UITableViewUiCollectionView被重用细胞的方式做。我认为这个问题在viewController's代码的某处和我的UItextField属性的实例..我虽然可能不在这里。

我知道我列出一个可能的解决方法 - 不过,我希望得到这个问题的底部,找到如何使用它的原因。和潜在的加扰的重新加载 -

+0

做你尝试给标签文本框? –

+0

我想过 - 我会在cellForRowAtIndexPath方法中设置标签吗? – Tander

+0

是....尝试cellforrowatindex –

回答

1

保持指针的UITableView/UICollectionView细胞的子视图因为滚动时将细胞再利用充满。重用后,您认为在一个索引路径中的子视图现在处于另一个索引路径中。

造成这种情况的最常见的解决方案是一个自定义单元格网点。其他人每次配置单元格时都会保持标签最新。其他人赞成在视图层次结构中快速搜索(比如说,按一下按钮)以确定子视图对应的索引路径。

但由于ReactiveCocoa是所有关于模型 - 视图连接它想要一双为二人指针。幸运的是,它还提供了与细胞重复使用相对应的结束条件。 This gist demonstrates如何为表格视图单元格的子视图建立活动连接。复制在这里......

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    UITableViewCell *cell = 
     [tableView dequeueReusableCellWithIdentifier:REUSABLE_CELL_ID]; 
    UILabel *label = (UILabel *)[cell viewWithTag:VIEW_TAG]; 
    Model *someModel = [self getModelFromIndexPath:indexPath]; 
    // `takeUntil:` makes the RACObserve() signal complete (and thus breaks the subscription) 
    // when the cell is recycled. 
    RAC(label, text) = [RACObserve(someModel, someKey) 
     takeUntil:cell.rac_prepareForReuseSignal]; 
    return cell; 
} 
相关问题