UITableView: How to dynamically change cell height when a button is clicked? swift

I can tell how to do this in objective-c from here , but how can I convert this to fast?

I have a UITableView with custom TableViewCells that has a UIButton named "expandButton". I am trying to figure out how to change the height of this particular cell when expandButton for that cell is clicked.

In addition, when it is clicked again, it should return to its original size. I am not familiar with ObjectiveC, so please help me with this in Swift. Thanks for the bunch in advance!

Here is what I still have:

    //Declaration of variables as suggested
        var shouldCellBeExpanded:Bool = false
        var indexOfExpendedCell:NSInteger = -1

Now inside the ViewController. Note. TableViewCell is the name of my custom cell.

    //Inside the ViewController
        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    if let cell:TableViewCell = TableViewCell() {

        cell.stopWatch = stopWatchBlocks[indexPath.row]

        cell.expandButton.tag = indexPath.row

        //Adding action to the expand button
        cell.expandButton.addTarget(self, action: "expandButtonAction1:", forControlEvents: UIControlEvents.TouchUpInside)

        return cell

    }

}

Now the button action method:

    func expandButtonAction1(button:UIButton) {

    button.selected = !button.selected

    if button.selected {

        indexOfExpendedCell = button.tag
        shouldCellBeExpanded = true

        self.TableView.beginUpdates()
        self.TableView.reloadRowsAtIndexPaths([NSIndexPath(forItem: indexOfExpendedCell, inSection: 0)], withRowAnimation: .Automatic)
        self.TableView.endUpdates()

        button.setTitle("x", forState: UIControlState.Selected)
    }

    else if !button.selected {

        indexOfExpendedCell = button.tag
        shouldCellBeExpanded = false

        self.TableView.beginUpdates()
        self.TableView.reloadRowsAtIndexPaths([NSIndexPath(forItem: indexOfExpendedCell, inSection: 0)], withRowAnimation: .Automatic)
        self.TableView.endUpdates()

        button.setTitle("+", forState: UIControlState.Normal)
    }
}

And finally, HeightForRowAtIndexPath

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

    if shouldCellBeExpanded && indexPath.row == indexOfExpendedCell {
        return 166.0
    }
    else { return 91.0 }
}

, - , , 91 -!

?

+4
1

, . , . mustCellBeExpanded:

func expandButtonAction1(button:UIButton) {

    shouldCellBeExpanded = !shouldCellBeExpanded
    indexOfExpendedCell = button.tag

    if shouldCellBeExpanded {
        self.TableView.beginUpdates()
        self.TableView.endUpdates()

        button.setTitle("x", forState: UIControlState.Normal)
    }

    else {
        self.TableView.beginUpdates()
        self.TableView.endUpdates()

        button.setTitle("+", forState: UIControlState.Normal)
    }
}

, , reloadRowsAtIndexPath . beginUpdates() endUpdates() , .

+7

Source: https://habr.com/ru/post/1629625/


All Articles