我有一個內部具有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 =零;
'self.textField.text = @ 「」'? – JAL 2014-12-04 15:07:55
問題出在用戶將編輯文本框的時候。例如,它會顯示一個DatePicker而不是數字鍵盤,可能是因爲該單元格之前是一個日期模型。 – DaSilva 2014-12-04 15:10:53
顯示日期選擇器代替文本字段而不是鍵盤的代碼。 – 2014-12-04 15:13:56