2015-08-17 29 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; 
} 
相關問題