当Ui Tableview滚动时,单元格文本内的自定义文本字段为空

时间:2017-03-03 04:36:51

标签: ios swift uitableview

enter image description here我在视图控制器中使用表视图。在我的表视图中有多个部分,而section = 0是自定义文本字段,当滚动表视图然后文本字段单元格文本重叠或空白时,如何解决 我的代码在下面给出

enter image description here

    if (indexPath.section == 0)
    {
        let cellidentifier="cell1"
        let cell=tableView.dequeueReusableCell(withIdentifier: cellidentifier,for:indexPath as IndexPath)  as! TextfieldTableViewCell

             cell.main_textfield_hieght.constant = CGFloat(main_tf_height)
            cell.main_textfield_leading.constant = CGFloat(main_tf_leading)
            cell.main_textfield_trailing.constant = CGFloat(main_tf_trailing)
            cell.main_textfield_width.constant = CGFloat(main_tf_width)


        let object:BaseClass = arr_baseclass[indexPath.row]
        cell.text_field.placeholder = object.fieldName
        cell.text_field.tag=indexPath.row+1
       cell.text_field.delegate=self
        return cell
    }

在给定的屏幕截图中,我在滚动位置不匹配后输入了文本Ist字段

2 个答案:

答案 0 :(得分:0)

如果文本字段超过圈数据

,请使用此代码
    let subViews = cell.contentView.subviews
    for subview in subViews
    {
      if (subview is UITextField) 
       { 
        subview.removeFromSuperview()
      }  
    }

答案 1 :(得分:0)

创建一个textFields数组,并在创建单元格时附加文本字段。

var arrOfTxtFields:[UITextField] = []

cellForRowAtIndexPath内删除所有子视图。

if (indexPath.section == 0)
    {
       let cellidentifier="cell1"
       let cell=tableView.dequeueReusableCell(withIdentifier: cellidentifier,for:indexPath as IndexPath)  as! TextfieldTableViewCell
       for subview in cell.contentView.subviews
       {
       subview.removeFromSuperview()
       }
       if arrOfTxtFields.indices.contains(indexPath.row)
       {
          cell.addSubview(arrOfTxtFields[indexPath.row])
       }
       else
       {
       let object:BaseClass = arr_baseclass[indexPath.row]
       cell.text_field.placeholder = object.fieldName
       cell.text_field.tag=indexPath.row+1
       cell.text_field.delegate=self
       arrOfTxtFields.append(cell.text_field)
       }
        return cell
    }