I have a custom cell with a UITextfield inside. This cell have different behaviors for different type of models. For example, i can have a model to present a number or other to present a date.
If is a number, it only can introduce numbers, if it´s a date when the user start to type on the textfield, a uipicker appear to select the date.
In the method cellForRow I set the textfield´s delegate to the model witch will implement each behavior for the cell.
- (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
}
My problem is when the table is reloaded, for example when the user start editing the textfield a date picker appears in a number of model. In my custom cell i try to clean when it will be reused, but no effect
-(void)prepareForReuse{
[super prepareForReuse];
[self.textField resignFirstResponder];
self.textField.delegate = nil;
}
Any ideas? Thanks in advance
If i set the inputView of the textfield to nil in the prepareForReuse´ method
self.textField.inputView = nil;
the picker don't appear. However i am adding a "Done" button in the datePicker, and that button still appear.
To remove the done button, just clean the inputAcessoryView of the textField self.textField.inputAcessoryView = nil;
You should use texfields
property inputView
, to set input mode. When you are setting delegate to nil, this property remains unchanged, so does the behaviour which you want to get rid off.
Documentation states:
If the value in this property is nil, the text field displays the standard system keyboard when it becomes first responder. Assigning a custom view to this property causes that view to be presented instead.
The default value of this property is nil.
Example: 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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With