【发布时间】:2018-09-11 14:31:35
【问题描述】:
如何在不使用委托方法的情况下限制UITextField 的字符。我使用UITableView,其中我在单元格中有UITextField,并在同一屏幕内的不同位置使用,我想将文本字段限制在某个位置UITableViewCell。任何帮助将不胜感激。
【问题讨论】:
标签: ios objective-c iphone
如何在不使用委托方法的情况下限制UITextField 的字符。我使用UITableView,其中我在单元格中有UITextField,并在同一屏幕内的不同位置使用,我想将文本字段限制在某个位置UITableViewCell。任何帮助将不胜感激。
【问题讨论】:
标签: ios objective-c iphone
前段时间我遇到了同样的问题。但是,不使用代表不是正确的方法。您应该使用委托并根据位置控制行为。
在你的 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
}
确保文本字段委托
【讨论】: