2014-12-04 37 views
5

我有一個內部具有UITextfield的自定義單元格。 對於不同類型的模型,此單元格具有不同的行爲。 例如,我可以有一個模型來呈現一個數字或其他來呈現日期。重複使用帶有文本字段的單元格

如果是數字,它只能引入數字,如果它是用戶開始在文本字段上鍵入的日期,則uipicker會選擇日期。

在方法cellForRow中,我將文本字段的委託設置爲模型女巫將實現單元格的每個行爲。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //Get Element 
    SuperElement * elem = [self.arrayFields objectAtIndex:indexPath.row]; 
    //Get Cell of the element 
    SuperCell *cell = (SuperCell*)[elem returnCellForCollectionView:collectionView andIndexPath:indexPath]; 

    return cell; 
} 

DateElement

-(UICollectionViewCell*)returnCelForCollectionView:(UICollectionView*)collectionView andIndexPath:(NSIndexPath*)indexPath{ 

    SuperCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[SuperCell reuseIdentifier] 
                    forIndexPath:indexPath]; 
    cell.textField.text = self.value; 
    cell.textField.delegate =self; 

return cell; } 

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ 
    //code to show picker 
} 

NumberElement

-(UICollectionViewCell*)returnCelForCollectionView:(UICollectionView*)collectionView andIndexPath:(NSIndexPath*)indexPath{ 

     SuperCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[SuperCell reuseIdentifier] 
                     forIndexPath:indexPath]; 
     cell.textField.text = self.value; 
     cell.textField.delegate =self; 

    return cell; 
} 
    - (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string 
{ 
    //reg to accept only numbers 
} 

我的問題是,當表被重新加載,例如,當用戶開始編輯日期選擇器出現在文本框一些模型。 在我的自定義單元格我試圖清理時,它會被重用,但沒有效果

-(void)prepareForReuse{ 

    [super prepareForReuse]; 
    [self.textField resignFirstResponder]; 
    self.textField.delegate = nil; 

} 

任何想法? 在此先感謝

編輯

如果我在prepareForReuse'方法

self.textField.inputView = nil; 

文本字段的inputView設爲零揀貨機不會出現。不過,我在datePicker中添加了「完成」按鈕,並且該按鈕仍然出現。

編輯2

要刪除完成按鈕,只是清潔文本字段的inputAcessoryView self.textField.inputAcessoryView =零;

enter image description here

+0

'self.textField.text = @ 「」'? – JAL 2014-12-04 15:07:55

+0

問題出在用戶將編輯文本框的時候。例如,它會顯示一個DatePicker而不是數字鍵盤,可能是因爲該單元格之前是一個日期模型。 – DaSilva 2014-12-04 15:10:53

+0

顯示日期選擇器代替文本字段而不是鍵盤的代碼。 – 2014-12-04 15:13:56

回答

1

您應該使用texfields財產inputView,設置輸入模式。當您將委託設置爲零時,此屬性保持不變,您希望擺脫的行爲也會保持不變。

文檔狀態:

如果在此屬性的值爲零,該文本字段顯示標準系統鍵盤當它成爲第一個響應者。爲該屬性分配自定義視圖會導致該視圖被呈現。

此屬性的默認值爲零。

實施例: DateElement

-(UICollectionViewCell*)returnCelForCollectionView:(UICollectionView*)collectionView andIndexPath:(NSIndexPath*)indexPath { 
    SuperCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[SuperCell reuseIdentifier] 
                    forIndexPath:indexPath]; 
    cell.textField.text = self.value; 
    cell.textField.delegate =self; 
    UIDatePicker *datePicker = [[UIDatePicker alloc] init]; 
    datePicker.datePickerMode = UIDatePickerModeDate; 
    cell.textField.inputView = datePicker; 

return cell; } 
0

嘗試這種情況:

- (UITextField *) textField { 

    if (!_textField) { 

      _textField = [[UITextField alloc] init.....]; 
      // Basically customize your textfield here 
    } 

    return _textField; 
} 


-(void)prepareForReuse { 

    [super prepareForReuse]; 

    [self.textField resignFirstResponder]; 

    [self.textField removeFromSuperView]; 
    self.textField = nil; 
} 
相關問題