DaS*_*lva 5 uitableview uitextfield ios uicollectionviewcell
我有一个内置UITextfield的自定义单元格.该细胞对不同类型的模型具有不同的行为.例如,我可以使用模型来呈现数字或其他来呈现日期.
如果是数字,它只能引入数字,如果是用户开始在文本字段上键入的日期,则显示选择日期的uipicker.
在方法cellForRow中,我将textfield的委托设置为模型,它将实现单元格的每个行为.
- (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;
}
Run Code Online (Sandbox Code Playgroud)
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
}
Run Code Online (Sandbox Code Playgroud)
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
}
Run Code Online (Sandbox Code Playgroud)
我的问题是当表重新加载时,例如当用户开始编辑文本字段时,日期选择器出现在多个模型中.在我的自定义单元格中,我尝试清理它何时会被重用,但没有效果
-(void)prepareForReuse{
[super prepareForReuse];
[self.textField resignFirstResponder];
self.textField.delegate = nil;
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?提前致谢
如果我在prepareForReuse方法中将textfield的inputView设置为nil
self.textField.inputView = nil;
Run Code Online (Sandbox Code Playgroud)
选择器没有出现.但是我在datePicker中添加了一个"完成"按钮,该按钮仍然出现.
要删除完成按钮,只需清理textField的inputAcessoryView self.textField.inputAcessoryView = nil;
小智 1
您应该使用texfieldsproperty inputView, 来设置输入模式。当您将 delegate 设置为 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; }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1005 次 |
| 最近记录: |