【问题标题】:Reusing cell with textfield重用带有文本字段的单元格
【发布时间】:2015-02-02 12:34:04
【问题描述】:

我有一个自定义单元格,里面有一个 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;
}

日期元素

-(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
}

数字元素

-(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´ 方法中将文本字段的 inputView 设置为 nil

self.textField.inputView = nil;

选择器没有出现。但是我在 datePicker 中添加了一个“完成”按钮,并且该按钮仍然出现。

编辑 2

要移除完成按钮,只需清理 textField 的 inputAcessoryView self.textField.inputAcessoryView = nil;

【问题讨论】:

  • self.textField.text = @""?
  • 问题是用户何时编辑文本字段。例如,它显示为 DatePicker 而不是数字键盘,可能是因为该单元格之前使用了日期模型。
  • 显示为该文本字段而不是键盘出现日期选择器的代码。
  • 试试self.textField.inputView = nil;
  • @MichaelDautermann 刚刚更新了我的问题

标签: ios uitableview uitextfield uicollectionviewcell


【解决方案1】:

您应该使用texfields 属性inputView 来设置输入模式。当您将委托设置为 nil 时,此属性保持不变,您想要摆脱的行为也是如此。

文档状态:

如果此属性中的值为 nil,则文本字段在成为第一响应者时显示标准系统键盘。将自定义视图分配给此属性会导致显示该视图。

此属性的默认值为 nil。

示例: 日期元素

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

【讨论】:

    【解决方案2】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-28
      相关资源
      最近更新 更多