Reusing a cell with a text box

I have a custom cell with a UITextfield inside. This cell has different types of behavior for different types of models. For example, I may have a model to represent a number or another to represent a date.

If it is a number, it can enter numbers only if it is the date when the user starts to enter text in the text box, the uipicker icon appears to select the date.

In the cellForRow method, I set the text field delegate to the model that 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 that when a table is reloaded, for example, when a user starts editing a text field, a set of date pickers appears in several models. In my custom cell, I try to clear when it is reused, but without effect

 -(void)prepareForReuse{ [super prepareForReuse]; [self.textField resignFirstResponder]; self.textField.delegate = nil; } 

Any ideas? thanks in advance

Edit

If I set the inputView of the text field to zero in the prepareForReuse method

 self.textField.inputView = nil; 

the collector does not appear. However, I add a Finish button to the datePicker, and that button still appears.

Edit 2

To remove the done button, simply clear the inputAcessoryView of the text field self.textField.inputAcessoryView = nil;

enter image description here

+5
source share
2 answers

To set the input mode, you must use the texfields inputView property. When you set the delegate to nil, this property remains unchanged, as does the behavior you want to get rid of.

The documentation reads:

If the value in this property is nil, the text field displays the standard system keyboard when it becomes the first responder. Assigning a custom view to this property causes the 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; } 
+1
source

Try the following:

 - (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; } 
0
source

Source: https://habr.com/ru/post/1208362/


All Articles