我有一个带有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;
}
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
}
我的问题是重新加载表时,例如,当用户开始编辑文本字段时,日期选择器会出现在许多模型中。在我的自定义单元格中,我尝试清理何时可以重复使用,但没有效果
-(void)prepareForReuse{
[super prepareForReuse];
[self.textField resignFirstResponder];
self.textField.delegate = nil;
}
有任何想法吗?提前致谢
如果我在prepareForReuse´方法中将文本字段的inputView设置为nil
self.textField.inputView = nil;
选择器不会出现。但是我在datePicker中添加了“完成”按钮,并且该按钮仍然出现。
要删除完成按钮,只需清除textField self.textField.inputAcessoryView = nil的inputAcessoryView即可;
您应该使用texfields
propertyinputView
来设置输入模式。当您将委托设置为nil时,此属性将保持不变,您想要摆脱的行为也将保持不变。
文档状态:
如果此属性中的值为nil,则文本字段在成为第一响应者时将显示标准系统键盘。将自定义视图分配给该属性将导致显示该视图。
此属性的默认值为nil。
示例: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; }
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句