【发布时间】: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