Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reusing cell with textfield

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

Edit

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.

Edit 2

To remove the done button, just clean the inputAcessoryView of the textField self.textField.inputAcessoryView = nil;

enter image description here

like image 678
DaSilva Avatar asked Nov 09 '22 22:11

DaSilva


1 Answers

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; }
like image 99
kedzia Avatar answered Dec 09 '22 02:12

kedzia