简体   繁体   English

如何在不重叠表视图中的其他单元格的情况下重用表视图单元格

[英]How to reuse a table view cell without overlapping other cells in tableview ios swift

I am getting the widgets/data overlapped in every cell when I scroll down or up my tableview. 当我向下或向上滚动表格视图时,我在每个单元格中重叠了小部件/数据。 What I am doing is, I have an array in elements are wCheckinArray = ["Date", "TextFeild", "TextView", "Check", "Sign"] . 我正在做的是,我在元素中有一个数组wCheckinArray = ["Date", "TextFeild", "TextView", "Check", "Sign"] Now I am checking if there is an element "anyElement" in wCheckinArray then I am making an anyElement in a cell and add as a subview in content view of a cell. 现在我检查是否有一个元素"anyElement"wCheckinArray然后我在做一个anyElement在细胞并添加在细胞中的内容视图中的子视图。

Example: 例:

Under cellForRowAtIndexPath method of UITableView : 在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



Real code: 实码:

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!
    }


This is how tableView looks when I scroll up or down the tableView 这是当我上下滚动tableView时tableView的外观

在此处输入图片说明


What I have Googled 我用Google搜索了什么

What I have googled is This question which suggests to me that I should check the clear graphics context which is done by default in Xcode 我用谷歌搜索的是这个问题 ,向我建议我应该check clear graphics context ,这在Xcode中是默认完成的

Also searched Here which suggest me to change the cell identifier for every cell, well how can I do? 还在此处搜索,建议我更改每个单元的cell标识符,那我该怎么办? How can I change the cell identifier which I have mentioned in Tableview cell's attributes inspector in Xcode. 如何更改在Xcode中的Tableview单元格attributes inspector中提到的单元格标识符。 Is there any solution to reuse this programatically. 是否有解决方案以编程方式重用此方法。

Update 更新资料

The tags of widget like textview.tag or textfield.tag are increasing when I scroll the tableview from bottom to upward here is how I printed the tag of textfield 当我从底部向上滚动表格视图时,诸如textview.tag或textfield.tag之类的小部件的标签会增加,这是我打印textfield的标签的方式

在此处输入图片说明

You better remove your all subviews creating subview.And make sure your y position to every elements is not same 最好删除所有子视图以创建子视图。并确保每个元素的y位置都不相同

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何加载单元格而不影响iOS中表视图的第一个单元格 - how to load cells without affecting the first cell of the table view in iOS Swift iOS-如何更改已在屏幕上加载的所有可见单元的TableView单元属性,而无需重新加载数据? - Swift iOS -How to change TableView Cell Properties for All Visible Cells Already Loaded On Screen Without Reloading Data? 防止在表视图Swift中重用自定义单元格 - Prevent reuse of custom cells in table view Swift 表格视图重叠的单元格swift2 - Table view overlapping cells swift2 如何在编辑 tableview 单元格中的文本字段时重新加载所有 tableview 单元格 - iOS Swift - How to reload all tableview cells, while editing a textfield in a tableview cell - iOS Swift 小部件在表视图swift ios的单元格中彼此重叠 - Widgets overlap each other in the cells of table view swift ios Swift 3-可扩展的表格视图单元格而不关闭其他单元格 - Swift 3 - Expandable table view cells without closing other ones 在静态表格视图中重用表格单元 - iOS - Swift - Reuse tablecell in static table view - iOS - Swift 如何在表视图单元格中获取此表视图在Swift 4中另一个表视图控制器中的文本字段文本? - how to get textfields texts in tableview cells that this tableview is in another table view controller in swift 4? 当不同的自定义视图作为调用添加到表中时,如何重用tableview中的单元格 - how to reuse a cell in tableview when different custom view add to the table as call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM