Displaying UIButton in UITableView when cell is selected

I want to add UIButton

to mine UITableViewCell

. My cells expand in height when selected. The button should only be displayed when didSelectRow

called in the extended area ... I have 4 which I want to fill in with different questions, forms and buttons. Do I have to subclass each cell?

This is my code:

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

}

      

+3


source to share


2 answers


You have to subclass every cell you want. And when the didSelectRow

cell is called and expanded button.hidden = false

it should be called



0


source


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



+2


source







All Articles