Question

I have a tableView with drop down custom cells and I have some views in this cell that when the cell is selected they have to turn to visible, I tried some implementations but nothing is working. Can you help me?

My custom cell class:

class daysWorkPointsCell: UITableViewCell {

@IBOutlet var dayOfMonth: UILabel
@IBOutlet var hourAndMinute: UILabel
@IBOutlet var greenBar: UIView
@IBOutlet var scroll: UIScrollView
@IBOutlet var hourView: UIView

// greenBar,scroll and hourView are invisible and I need to turn to visible when selected

init(style: UITableViewCellStyle, reuseIdentifier: String!) {
    super.init(style: UITableViewCellStyle.Value1, reuseIdentifier: reuseIdentifier)
}

}

My view Controller:

class DayViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet var daysWorkPointTable: UITableView

var selectedCellIndexPath: NSIndexPath?
let SelectedCellHeight: CGFloat = 150.0
let UnselectedCellHeight: CGFloat = 75.0

override func viewDidLoad() {
    super.viewDidLoad()

    var nipName = UINib(nibName: "daysWorkPointsCell", bundle: nil)

    self.daysWorkPointTable.registerNib(nipName, forCellReuseIdentifier: "daysWorkCell")


}

func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
    return 1
}

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!){

    if let selectedCellIndexPath = selectedCellIndexPath {

        if selectedCellIndexPath == indexPath {
            self.selectedCellIndexPath = nil
        } else {
            self.selectedCellIndexPath = indexPath
        }

    } else {
        selectedCellIndexPath = indexPath
    }

    tableView.beginUpdates()
    tableView.endUpdates()

}

func tableView(tableView:UITableView!, heightForRowAtIndexPath indexPath:NSIndexPath)->CGFloat {

    if let selectedCellIndexPath = selectedCellIndexPath {
        if selectedCellIndexPath == indexPath {

            return SelectedCellHeight
        }
    }

    return UnselectedCellHeight
}

// here I tried to change the visibility but don't work
    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{

    var cell = tableView.dequeueReusableCellWithIdentifier("daysWorkCell", forIndexPath: indexPath) as daysWorkPointsCell

    if let selectedCellIndexPath = selectedCellIndexPath {
        if selectedCellIndexPath == indexPath {

            cell.greenBar.hidden = false
            cell.scroll.hidden = false
            cell.hourView.hidden = false
        }else{
            cell.greenBar.hidden = true
            cell.scroll.hidden = true
            cell.hourView.hidden = true
        }
    }

    return cell
}

}

Was it helpful?

Solution

No need to selectedIndexPath Use instead below code it is much cleaner

override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!)
{


    for cell in tableView.visibleCells(){
          var hidden:Bool = cell.greenBar.hidden
    cell.greenBar.hidden  = !hidden

    hidden = cell.scroll.hidden
    cell.scroll.hidden  = !hidden

    hidden = cell.hourView.hidden
    cell.hourView.hidden  = !hidden
    }

    var cell = tableView.cellForRowAtIndexPath(indexPath)

    var hidden:Bool = cell.greenBar.hidden
    cell.greenBar.hidden  = !hidden

    hidden = cell.scroll.hidden
    cell.scroll.hidden  = !hidden

    hidden = cell.hourView.hidden
    cell.hourView.hidden  = !hidden


}

I have used hidden variable here instead of cell.greenBar.hidden = !cell.greenBar.hidden because in swift ! is also used for unwrapping so it creates some problems when used with optionals

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top