Ios textfield中的swift 3 tableView会复制自身

Ios textfield中的swift 3 tableView会复制自身,ios,iphone,swift,xcode,swift3,Ios,Iphone,Swift,Xcode,Swift3,我的第一行文章 把自己复制到底线 当我滚动时, 数据的位置正在改变 UITableView单元格中的数据是重复的,因为单元格被重用,所以您需要跟踪单元格的数据,可能您可以在按索引排列的数组中添加数据 是的,这就是UITableview dequeCells的概念:因为它重用单元格,所以如果有100个条目,那么一次只能创建有限数量的单元格以节省内存。 现在为了避免这种情况,请使用包含所有值的数据源填充单元格视图。 如果您看到数据源中缺少一些值,请使用,例如: func tableView(_ t

我的第一行文章 把自己复制到底线

当我滚动时, 数据的位置正在改变


UITableView单元格中的数据是重复的,因为单元格被重用,所以您需要跟踪单元格的数据,可能您可以在按索引排列的数组中添加数据

是的,这就是UITableview dequeCells的概念:因为它重用单元格,所以如果有100个条目,那么一次只能创建有限数量的单元格以节省内存。 现在为了避免这种情况,请使用包含所有值的数据源填充单元格视图。 如果您看到数据源中缺少一些值,请使用,例如:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! Tek101TableViewCell
    cell.selectionStyle = .none
    cell.siraLabel.text = ""
    cell.siraLabel.text = String(indexPath.row + 1) + ")"
    return cell
}

您需要在textfield delegate方法中设置如下值:(根据您的需要)

如果需要,请使用键盘上的返回按钮

       func textFieldShouldReturn(_ textField: UITextField) -> Bool {
            let cell = textField.superview?.superview as! Tek101TableViewCell // track you view hierarchy
            cell. siraLabel?.text = textField.text
            textField.resignFirstResponder()
            return true
        }
func textFieldDidEndEditing(_ textField: UITextField){
    let cell = textField.superview?.superview as! Tek101TableViewCell // track you view hierarchy
    cell. siraLabel?.text = textField.text
}
如果要跟踪值结束编辑方法

       func textFieldShouldReturn(_ textField: UITextField) -> Bool {
            let cell = textField.superview?.superview as! Tek101TableViewCell // track you view hierarchy
            cell. siraLabel?.text = textField.text
            textField.resignFirstResponder()
            return true
        }
func textFieldDidEndEditing(_ textField: UITextField){
    let cell = textField.superview?.superview as! Tek101TableViewCell // track you view hierarchy
    cell. siraLabel?.text = textField.text
}

UITablview
中,单元格被重用,因此这种情况将会发生。要解决此问题,必须将所有记录保存在一个全局数组变量中

用户插入时,可以将所有输入存储在数组或字典中。然后你必须给
CellForRow
中的每个单元格赋予这个数组值

简而言之,您只需要处理数组


希望这能对你有所帮助。

这是因为你的细胞被重复使用了。具有值
1,2,3
的单元格现在被重新使用,它们仍然具有第一次保存在其中的值。如果保留以前条目中的值,请单独保存它们,并在返回
cellForRowAtIndexPath
中的单元格时,检查是否已在其中保存值。添加cellForRowAtIndexPath方法的代码我的回答有详细说明:Zion Perez我不再复制,但数据丢失没有。。。不要直接这样写。但是像这样写,
cell.Textfield.text=sell.myArray[0]as!String
例如,在此之前,必须使用实际值创建数组。如果没有值,则在该数组中添加默认值。然后在cellforrow.var MyText:[字符串?]中分配此数组!func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{print(“cellForRowAt:+indexPath.row.description”)让cell=tableView.dequeueReusableCell(带标识符:“cell”,for:indexath)作为!Tek101TableViewCell.oyunc1textfield.text=mytext[indexPath.row]返回单元}