如果有多个相同类型的节,则在展开表视图中覆盖节行数据

时间:2019-03-11 06:03:44

标签: swift uitableview

我使用节创建了可折叠表格视图,该表格依次包含一行。最初的表格视图有4个节,当用户点击最后一个时,我的最后一个节可以多次添加第3个节。

现在,当用户扩展该部分并输入数据时,他面临一个问题,之后,当他扩展动态添加的部分时,相同的数据也仅出现在新添加的部分中;如果用户修改了任何数据,反之亦然在新添加的部分行上,那么它也将覆盖第3部分中的现有内容。

代码如下:

override func viewDidLoad() {
        super.viewDidLoad()

  //creating section items where 1st three are static and the third section will add dynamically when the last section tapped by the user.
  tableMenuItems = [["sectionHeader": "Ship To","isCollapsed":true, "image" : "shipto.png" ],
                    ["sectionHeader": "Ship From","isCollapsed":true,"image" : "shipfrom.png"],
                    ["sectionHeader": "Package Details","isCollapsed":true,"image" : "package.png"],
                    ["sectionHeader": "Add a Package","isCollapsed":true,"image" : "newaddpack.png"]]
}

func numberOfSections(in tableView: UITableView) -> Int {
    return tableMenuItems.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    let isCollapsed = tableMenuItems[section]["isCollapsed"] as! Bool
    return isCollapsed ? 0 : 1
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{    
   let view = UIView.init(frame: CGRect.init(x: 0, y: 0, width: tableView.frame.size.width, height: 120))
   view.tag = 200 + section
   view.backgroundColor = .white
   let prefixIconBtn = UIButton.init(frame: CGRect.init(x: 12, y: 20, width: 48, height: 48))
   prefixIconBtn.setImage(UIImage.init(named: tableMenuItems[section]["image"] as! String), for: .normal)
   prefixIconBtn.tag = 500 + section
   prefixIconBtn.addTarget(self, action: #selector (displayOrHideSection(_sender:)), for: .touchUpInside)

   let titleLabel = UILabel.init(frame: CGRect.init(x: 75, y: 18, width: 250, height: 60))
   titleLabel.tag = 100 + section

   let sectionHeaderBtn = UIButton.init(frame: titleLabel.frame)
   sectionHeaderBtn.addTarget(self, action: #selector (displayOrHideSection(_sender:)), for: .touchUpInside)
   sectionHeaderBtn.tag = section

   titleLabel.font = UIFont(name:"SFUIText-Regular", size: 20.0)

   let plusOrMinus = UIButton.init(frame: CGRect.init(x: 340, y: 20, width: 38, height: 38))
   plusOrMinus.tag = section
   plusOrMinus.addTarget(self, action: #selector (displayOrHideSection(_sender:)), for: .touchUpInside)
   titleLabel.text = tableMenuItems[section]["sectionHeader"] as? String

   if titleLabel.text == "Add a Package" {
      plusOrMinus.isHidden = true   
   }

   if tableMenuItems[section]["isCollapsed"] as! Bool == true
   {
      plusOrMinus.setBackgroundImage(UIImage.init(named: "Log_plus"), for: .normal)
     tableMenuItems[section]["isCollapsed"] = true
   }
  else
  {
     plusOrMinus.setBackgroundImage(UIImage.init(named: "Log_minus"), for: .normal)
     tableMenuItems[section]["isCollapsed"] = false
  }

  view.addSubview(titleLabel)
  view.addSubview(prefixIconBtn)
  view.addSubview(sectionHeaderBtn)
  view.addSubview(plusOrMinus)

  plusOrMinus.tag = section

  let seperatorLine = UILabel.init(frame: CGRect.init(x: 78, y: 78, width: tableView.frame.size.width, height: 1))
  seperatorLine.backgroundColor = UIColor.lightGray
  view.addSubview(seperatorLine)    
  return view
}

@objc func displayOrHideSection( _sender : AnyObject)
{       
    let section = _sender.tag        
    if section == 0
    {
        if tableMenuItems[0]["isCollapsed"] as! Bool == false
        {
            tableMenuItems[0]["isCollapsed"] = true
        }
        else
        {
            tableMenuItems[0]["isCollapsed"] = false
        }
    }

    else if section == 1
    {
        if tableMenuItems[1]["isCollapsed"] as! Bool == false
        {
            tableMenuItems[1]["isCollapsed"] = true
        }
        else
        {
            tableMenuItems[1]["isCollapsed"] = false
        }
    }
    else if section == 2
    {
        if tableMenuItems[2]["isCollapsed"] as! Bool == false
        {
            tableMenuItems[2]["isCollapsed"] = true
        }
        else
        {
            tableMenuItems[2]["isCollapsed"] = false
        }
    }        
    else if section! >= 3
    {                       
        if section!  > 502

        {
            // to delete a dynamically added  package
            tableMenuItems.remove(at: section! - 500)               
        }
        else if section!  < 502              
        {               
            // adding a new package section when the user taps on Add a package section(Last section)

        let titleLabel  = self.logTable?.viewWithTag(100 + section!) as! UILabel

        print(titleLabel)

        // when add a package tapped

        if titleLabel.text == "Add a Package"
        {
            tableMenuItems.insert(["sectionHeader": "Package Details","isCollapsed":true,"image" : "package.png"], at: tableMenuItems.count-1)
        }

      // when plus or minus button tapped on Add a package
      else
      {

        if tableMenuItems[section!]["isCollapsed"] as! Bool == false
        {
             // expanding section

             tableMenuItems[section!]["isCollapsed"] = true
             tableMenuItems[section!]["image"] = "package.png"


        }
        else
        {
             // collapsing section

            tableMenuItems[section!]["isCollapsed"] = false
            tableMenuItems[section!]["image"] = "newdeletepackage.png"             
        }

        }

      }
    }
 logTable!.reloadData()

}

0 个答案:

没有答案