如何在不使用委托方法的情况下限制UITextField
的字符。IM使用UITableView
,其中我在单元格中具有UITextField
并在同一屏幕中使用的不同位置使用,我想限制某些位置UITableViewCell
的文本字段。任何帮助将不胜感激。
我在一段时间前遇到了同样的问题。但是,不使用代表不是正确的方式。您应该根据位置使用代表并控制行为。
-
在您的tableviewcell类中发挥功能,假设是
-(void)setMyCellTag:(NSInteger)tag { self.tag = tag; }
-
现在从您的 cellforrowatIndExpath 函数为,
[cell setMyCellTag:indexPath.row];
-
在uitextfield委托法中使用条件,
if(self.tag == yourposition) { //Do Something }
简单。感谢您的阅读。
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
{
let currentCharacterCount = textField.text?.characters.count ?? 0
if (range.length + range.location > currentCharacterCount){
return false
}
let newLength = currentCharacterCount + string.characters.count - range.length
return newLength <= 1 // set your limit
}
确保Textfield委托