gpt4 book ai didi

ios - 在 iOS 中设置 tableView 内的 editText 长度限制

转载 作者:行者123 更新时间:2023-11-30 10:58:52 25 4
gpt4 key购买 nike

我使用 tableView 作为注册屏幕,我需要对每一行强制执行一些长度限制。我使用了下面的函数,但它让所有字段都具有相同的约束。有谁知道如何处理?

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let maxLength = 12
let currentString: NSString = textField.text! as NSString
let newString: NSString =
currentString.replacingCharacters(in: range, with: string) as NSString
return newString.length <= maxLength
}

最佳答案

由于您的文本字段委托(delegate)方法位于单元格中,因此单元格需要知道对其文本字段强制执行的长度。

您的 View Controller 需要根据当前索引路径为 cellForRowAt 中的每个单元格提供该长度。

向单元格类添加 textLength 属性,然后根据需要在 cellForRowAt 中设置该值。

然后,单元格类中的 shouldChangeCharactersIn 可以使用该 textLength 属性,而不是现在的硬编码 12

关于ios - 在 iOS 中设置 tableView 内的 editText 长度限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53635904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com