如果我在 UITableView
中有很多帐户。当我滚动 UITableView
时,我在第一个单元格中编辑的 UITextFid
文本正在重置。这是因为单元可重用性而发生的。我没有在索引路径的 cellForRow 的 UITableView
中设置我的 UITextFid
文本。由于其他实现,我在自定义单元格 View 中执行此操作。
你能指导我吗?
STTableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:@"STTableViewCell"];
if (cell == nil)
{
cell = [[STTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"STTableViewCell"];
}
cell.accountNameTxtField.delegate = self
// Fetching the URL from Array at index of indexpath.row
OTPAuthURL *url = [self.authURLs objectAtIndex:indexPath.row];
// Sending the generated authURl and token description to custom STTableViewCell to load the data in a tableView
[(STTableViewCell *)cell setAuthURL:url withtokenDesc:nil];
return cell;
最佳答案
您需要维护一个数组,其中的对象数等于 TableView 中的项目数。 每个文本字段都应该有一个等于该行的 indexPath.row 的标签。 例如:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:CustomUITableViewCell = tableView.dequeueReusableCell(withIdentifier: "customCell")! as CustomUITableViewCell
cell.textfield = indexPath.row
cell.textfield.delegate = self
cell.textfield.text = arrayTemp[indexPath.row] as String
}
}
使用文本字段的委托(delegate)方法你将拥有
public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool{
arrary[textfield.tag] = textfield.text
}
在文本字段委托(delegate)方法中,您需要将文本保存在临时数组中的索引等于文本字段标签的位置
关于ios - 自定义单元格中的 UITextField 文本在滚动 UITableView 时发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41737547/