Search code examples
iosuitableviewswiftsections

how to add a second section in a tableView depending on a bool value Swift


I'm trying to add a second section to my tableView. I want to be able to transfer the row from section 1 when the checkbox inside the cell is pressed and make it appear in the second section and vice versa.

I searched on the forum and found only how to sort sections by date.

Please check my commented lines to understand where I have questions.

here is what I have

var cellItemData = [String]() //this is my main table data
var checkedCells = Array(count: cellItemData.count, repeatedValue:false) // this is where I assign false to all my unchecked rows in section 0
var secondSectionData = [String]() //this is where I store the cells for the second section



func numberOfSectionsInTableView(tableView: UITableView) -> Int {
   return 2
 }

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) ->     Int{

       if section == 0 {
     return cellItemData.count
                       }
       if section == 1 {
     secondSectionData.count
                       } 
       return 0
   }

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> TableViewCell {

        var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as TableViewCell       

           if checkedCells[indexPath.row] == false{

        cell.checkBoxImage.setImage(UIImage(named:"unchecked"),forState:UIControlState.Normal)

        } else if checkedCells[indexPath.row] == true {

        cell.checkBoxImage.setImage(UIImage(named:"checked"),forState:UIControlState.Normal)

        }

       cell.textLabel?.text = cellItemData[indexPath.row]
       // what about the cells in the other section?

       cell.checkBoxImage.tag = indexPath.row

       cell.checkBoxImage?.addTarget(self, action: "buttonTouched:", forControlEvents: UIControlEvents.TouchUpInside)

    return cell
}

 func buttonTouched(sender: UIButton){

     for item in cellItemData {
        var i = 0
        if checkedCells[i] == true
        {
            secondSectionData.append(item)

            //tableView!.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection:1)], withRowAnimation: .Automatic)
            //self.tableView.reloadData()
            //not sure if this is the right way
            i++

        }
    }

   let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: sender.tag, inSection: 0)) as TableViewCell
    cellItemData.removeAtIndex(sender.tag)
    checkedCells.removeAtIndex(sender.tag)
    tableView!.deleteRowsAtIndexPaths([NSIndexPath(forRow: sender.tag, inSection:0)], withRowAnimation: .Fade)
    self.tableView.reloadData()

  }

Solution

  • You can do something like this:

    In

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
       return 2
     }
    

    add condition so you will have :

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
       if( secondSectionData && secondSectionData.count > 0)
           return 2
       else
           return 1
     }
    

    For your concern in the comment:

    // what about the cells in the other section?

    This method will go through all sections and rows. How to get then your data here? well, simply check if indexPath.section is 1 or 2.

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> TableViewCell {
    
            var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as TableViewCell       
    
               if checkedCells[indexPath.row] == false{
    
            cell.checkBoxImage.setImage(UIImage(named:"unchecked"),forState:UIControlState.Normal)
    
            } else if checkedCells[indexPath.row] == true {
    
            cell.checkBoxImage.setImage(UIImage(named:"checked"),forState:UIControlState.Normal)
    
            }
           if(indexPath.section== 1)
               cell.textLabel?.text = cellItemData[indexPath.row]
           else
               cell.textLabel?.text = secondSectionData[indexPath.row]
           // what about the cells in the other section?
    
           cell.checkBoxImage.tag = indexPath.row
    
           cell.checkBoxImage?.addTarget(self, action: "buttonTouched:", forControlEvents: UIControlEvents.TouchUpInside)
    
        return cell
    }