BENQ
BENQ

Reputation: 93

Display UIButton in UITableView , when Cell is selected

I Want to add a UIButton to my UITableViewCell.My cells expands in height when it is selected.. The button must only be displayed when didSelectRow is called in the expanded area ... I have 4 cells that I want to populate with different questions, forms and buttons. Should I sub-class each cell?

This is my code so far:

let SelectedCellHeight: CGFloat = 500.0
let UnselectedCellHeight: CGFloat = 44.0
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let logItemCell = tableView.dequeueReusableCellWithIdentifier("LogCell", forIndexPath: indexPath) as! UITableViewCell




}

// Used to expand cell when selected
func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
    if let selectedCellIndexPath = selectedCellIndexPath {
        if selectedCellIndexPath == indexPath {
            return SelectedCellHeight
        }
    }
    return UnselectedCellHeight
}


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()

}

Upvotes: 3

Views: 280

Answers (2)

000
000

Reputation: 225

You should subclass each cell that you want. And when didSelectRow is called and the cell is expanded button.hidden = false should be called

Upvotes: 0

Gwendle
Gwendle

Reputation: 1941

Yes, you should make 4 prototype cells with different identifier and class and return the one you want in 'cellForRowAtIndexPath' depending on the indexPath.

Upvotes: 2

Related Questions