【问题标题】:How can i hide a Section when UISwitch is Changed更改 UISwitch 时如何隐藏部分
【发布时间】:2016-08-05 09:58:48
【问题描述】:

这是我的tableView,想隐藏“eventdays”部分 我该怎么做?

希望你能帮助我。

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    // set data within item list
    self.going = ["You can go?","Whos going"]
    self.eventdays = ["Monday", "Sunday", "Wednesday"]
    self.others = ["Bread", "Butter", "Paneer"]

    // set table view delegate and data source
    self.myTableView.delegate = self
    self.myTableView.dataSource = self
    }

   override func didReceiveMemoryWarning() {
     super.didReceiveMemoryWarning()
     // Dispose of any resources that can be recreated.
   }



  // Mark: - Table view data source and delegate

  // set number of sections within table view
 func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 3
 }

 // set number for rows for each setion
 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0 {
        return self.going.count
    }
    if section == 1 {
        return self.eventdays.count
    }
    if section == 2 {
        return self.others.count
    }

    return 0
 }

 // set header title for each section
 func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    if section == 0 {
        return "Can Go?"
    }
    if section == 1 {
        return "Days"
    }
    if section == 2 {
        return "Others"
    }

    return "Default Title"
}

// set cell content for each row
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    // deque reusable cell
    let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as UITableViewCell
    let row = indexPath.row



    //Configure the switches for swipe options
    let teilnehmenSwitch = UISwitch(frame:CGRectMake(150, 300, 0, 0));
    teilnehmenSwitch.on = false

    let eventDaySwitch = UISwitch(frame:CGRectMake(150, 300, 0, 0));
    eventDaySwitch.on = false




    switch(indexPath.section){
    case 0:
        cell.textLabel?.text = going[row]
        if row == 0 {

            cell.accessoryView = teilnehmenSwitch

        }

    case 1:
        cell.textLabel?.text = eventdays[row]
        cell.accessoryView = eventDaySwitch
    case 2:
        cell.textLabel?.text = others[row]
    default:
        cell.textLabel?.text="Others"
    }

    // return cell
    return cell
 }

我怎样才能找出改变了什么开关,以便我可以将数据保存在DataBase

提前致谢

【问题讨论】:

    标签: ios swift uitableview switch-statement tableview


    【解决方案1】:

    您可以通过以下代码更改开关操作以获得更优化的重新加载解决方案

    @IBAction func switchValueChange(sender: UISwitch) {
        //tableView.reloadData()
        tableView.reloadSections(NSIndexSet(index: 0), withRowAnimation: .None)
    } 
    

    【讨论】:

      【解决方案2】:

      尝试以这种方式更改您的代码,使用您的 UISwitch ValueChanged 事件添加一个操作并重新加载 tableView

      @IBAction func switchValueChange(sender: UISwitch) {
          tableView.reloadData()
      }    
      

      现在像这样更改您的 UITableViewDataSource 方法

      func numberOfSectionsInTableView(tableView: UITableView) -> Int {
          if mySwitch.on {
              return 3
          }
          else {
              return 2
          }
      }
      
      // set number for rows for each setion
      func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          if section == 0 {
              return self.going.count
          }
          if section == 1 {
              if mySwitch.on {
                  return self.eventdays.count
              }
              else {
                  return self.others.count
              }
          }
          if section == 2 {
              return self.others.count
          }
      
          return 0
      }
      
      func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
          if section == 0 {
              return "Can Go?"
          }
          if section == 1 {
              if mySwitch.on {
                  return "Days"
              }
              else {
                  return "Others"
              }
          }
          if section == 2 {
              return "Others"
          }
          return "Default Title"
      }
      

      【讨论】:

      • @xyzcode 你能用新代码编辑问题或用你的问题添加新问题并将链接发送到这里,这样很容易理解。 :)
      【解决方案3】:
      var  teilnehmenSwitch = UISwitch()
      var  eventDaySwitch = UISwitch()
      
      func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
      
      teilnehmenSwitch = UISwitch(frame:CGRectMake(150, 300, 0, 0));
      teilnehmenSwitch.addTarget(self, action: #selector(SettingVC.switchDidChangeteilnehmenSwitch(_:)), forControlEvents: .ValueChanged)
      teilnehmenSwitch.on = false
      
      eventDaySwitch = UISwitch(frame:CGRectMake(150, 300, 0, 0));
      eventDaySwitch.addTarget(self, action: #selector(SettingVC.switcheEventDaySwitch(_:)), forControlEvents: .ValueChanged)
      eventDaySwitch.on = false
      
      }
      
      func switchDidChangeteilnehmenSwitch(sender:UISwitch!)
      {
          if (sender.on == true){
              print("on")
          }
          else{
              print("off")
      
          }
      }
      
      func switcheEventDaySwitch(sender:UISwitch!)
      {
          if (sender.on == true){
              print("on")
      
          }
          else{
              print("off")
      
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多