如何重用表格视图单元格而又不重叠表格视图iOS Swift中的其他单元格

时间:2018-07-24 06:19:28

标签: ios swift uitableview reuseidentifier

当我上下滚动表格视图时,我在每个单元格中重叠了小部件/数据。我正在做的是,我有一个元素为wCheckinArray = ["Date", "TextFeild", "TextView", "Check", "Sign"]的数组。现在,我正在检查"anyElement"中是否有元素wCheckinArray,然后在单元格中制作一个anyElement,并将其作为子视图添加到单元格的内容视图中。

示例:

在UITableView的cellForRowAtIndexPath方法下:

  let w = wCheckinArray[indexPath.row]
  if w.contains(Date) {
    // make a date picker  and add it as a subview to content view of cell 
  }
    // and so on



真实代码:

var Cell : UITableViewCell?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    print("cellforrowatindexpathruns")
    var instockSignView : Int = 1
    Cell = tempCheckinTable.dequeueReusableCell(withIdentifier: "tempCheckinCell")! as UITableViewCell

    if wCheckinArray.count == 0 && lCheckinArray.isEmpty {
      print("no textFields in Checkin")
    }
    else 
    {
      print("widget process i in winstock loop")
      let w = wCheckinArray[indexPath.row]

      if w.contains(wTextField)  {
         print("textField was placed")
         newLabel = UITextField(frame: CGRect(x: 10, y: 10, width: 250, height: 30))
         newLabel.textColor = UIColor.gray
         newLabel.tag = widgetTagOk     
         print("You have a instock textLabel with tag \(newLabel.tag)")

         if lCheckinArray.isEmpty || idCheckinArray.count != lCheckinArray.count {
            newLabel.text = " "
         } else {
            newLabel.placeholder = lCheckinArray[indexPath.row]
            newLabel.layer.cornerRadius = 3
            newLabel.layer.borderColor = UIColor.gray.cgColor
            newLabel.layer.borderWidth = 1

            //instockTextField += 1
            widgetTagOk += 1
            print("\(wCheckinArray.count)")
            Cell?.contentView.addSubview(newLabel)
                }
            }

            if w.contains(wTextView) {
                print("textView was placed")
                newTextView = UITextView(frame: CGRect(x:10, y: 35, width: 250, height: 50))
                newTextView.textAlignment = NSTextAlignment.justified
                newTextView.textColor = UIColor.black
                newTextView.backgroundColor = UIColor.white
                newTextView.font = UIFont.systemFont(ofSize: 15)
                newTextView.tag = widgetTagOk //instockTextView

                print("You have a instock textView with tag \(newTextView.tag)")
                if lCheckinArray.isEmpty || idCheckinArray.count != lCheckinArray.count {
                } else {
                    textViewLabel = UILabel(frame: CGRect(x:10, y: 10, width: 250, height: 20))
                    textViewLabel.text = "\(lCheckinArray[indexPath.row]):"
                    textViewLabel.textColor = .black
                    textViewLabel.tag = widgetTagOk
                    print("here is the TextView place text  \(lCheckinArray[indexPath.row])")
                    newTextView.layer.cornerRadius = 3
                    newTextView.layer.borderColor = UIColor.gray.cgColor
                    newTextView.layer.borderWidth = 1
                    widgetTagOk += 1 //instockTextView += 1
                    print("\(wCheckinArray.count)")
                    Cell?.contentView.addSubview(newTextView)
                    Cell?.contentView.addSubview(textViewLabel)

                }
            }

            if w.contains(wSign) { //wInstockArray.contains(wSign) {
                print("sign was place   d")
                newSignView = UIView(frame: CGRect(x:10, y: 35, width: 250, height: 50))
                newSignView.backgroundColor = UIColor.white
                newSignView.tag = instockSignView
                print("You have a instock signView with tag \(newSignView.tag)")
                if lCheckinArray.isEmpty || idCheckinArray.count != lCheckinArray.count {
                    print("signview can nit be added as a widget")
                } else {
                    textViewLabel = UILabel(frame: CGRect(x:10, y: 10, width: 250, height: 20))
                    textViewLabel.text = "\(lCheckinArray[indexPath.row]):"
                    textViewLabel.textColor = .black
                    textViewLabel.tag = widgetTagOk
                    print("here is the TextView place text  \(lCheckinArray[indexPath.row])")
                    newSignView.layer.cornerRadius = 3
                    newSignView.layer.borderColor = UIColor.gray.cgColor
                    newSignView.layer.borderWidth = 1
                    instockSignView += 1
                    print("\(wCheckinArray.count)")
                    print("signview has added as a instock widget")
                    Cell?.contentView.addSubview(newSignView)
                    Cell?.contentView.addSubview(textViewLabel)
                }
            }


            if w.contains(wDate) { //wInstockArray.contains(wSign) {
                print("date was placed")
                newDatePicker = UIDatePicker(frame: CGRect(x:10, y: 35, width: 250, height: 50))
                newDatePicker.backgroundColor = UIColor.white
                newDatePicker.tag = instockSignView
                print("You have a checkin date picker with tag \(newSignView.tag)")
                if lCheckinArray.isEmpty || idCheckinArray.count != lCheckinArray.count {
                    print("date picker can nit be added as a widget")
                } else {
                    textViewLabel = UILabel(frame: CGRect(x:10, y: 10, width: 250, height: 20))
                    textViewLabel.text = "\(lCheckinArray[indexPath.row]):"
                    textViewLabel.textColor = .black
                    textViewLabel.tag = widgetTagOk
                    print("here is the date picker place text  \(lCheckinArray[indexPath.row])")
                    newDatePicker.layer.cornerRadius = 3
                    newDatePicker.layer.borderColor = UIColor.gray.cgColor
                    newDatePicker.layer.borderWidth = 1
                    instockSignView += 1
                    print("\(wCheckinArray.count)")
                    print("date picker has added as a checkin widget")
                    Cell?.contentView.addSubview(newDatePicker)
                    Cell?.contentView.addSubview(textViewLabel)
                }
            }

            // } //for i in instockarr comment bracket
            print("could not add widgets")
            print("here is w id \(idCheckinArray),here is w name: \(wCheckinArray), here is w data \(lCheckinArray)")
        }
    return Cell!
    }


这是我向上或向下滚动tableView时的tableView外观

enter image description here


我搜索过的内容

我用Google搜索的是This question,这提示我应该check clear graphics context,这在Xcode中是默认完成的

还搜索了Here,建议我为每个单元格更改cell标识符,那我该怎么办?如何更改Xcode中Tableview单元格attributes inspector中提到的单元格标识符。是否有任何解决方案可以以编程方式重用此方法。

更新

当我从底部向上滚动表格视图时,诸如textview.tag或textfield.tag之类的小部件的标签会增加,这是我打印textfield的标签的方式

enter image description here

1 个答案:

答案 0 :(得分:1)

最好删除所有创建子视图的子视图。并确保每个元素的 y位置不相同

Cell = tempCheckinTable.dequeueReusableCell(withIdentifier: "tempCheckinCell")! as UITableViewCell

    for subView in cell.contentView.subviews{
                subView .removeFromSuperview()
            }