0

Using Swift4, iOS11.1, Xcode9.1,

I would like to achieve dynamic height-change of one tableView-cell. The problem is as follows: Each cell in my tableView contains a UIView (called MyCellView). Upon user-touching of a cell this MyCellView of the touched cell shall change its height!

I successfully implemented a UIGestureRecognizer that brings me the action. In the action-method I change the height of the touched cell (so far so good). The view actually changes its height.

But unfortunately, if the MyCellView's new height is bigger than the original cell-height than the MyCellView is clipped off !

How can I dynamically achieve a cell-height change of a single cell in a tableView ?

Here is my gesture-recognizer action code:

@objc func toWebviewTouch(_ sender : UITapGestureRecognizer) {

    self.MyCellView.frame = CGRect(x: self.MyCellView.frame.origin.x, 
                                   y: self.MyCellView.frame.origin.y,
                               width: self.MyCellView.frame.width,
                              height: self.myNewHeight)

    self.contentView.bringSubview(toFront: self.MyCellView)

    self.contentView.setNeedsLayout()
    self.setNeedsUpdateConstraints()
    self.updateConstraintsIfNeeded()
}

I also followed the ideas of this link. But this only seems to change the tableView cell's height upon tableView-filling (and not once the tableView is established and the user touches cells).

Any help appreciated !

iKK
  • 6,394
  • 10
  • 58
  • 131

1 Answers1

0

Oh, I've found a solution:

With the help of this link, I've achieved my goal!

The key was to use the following inside 'didSelectRowAt'

tableView.beginUpdates()
tableView.endUpdates()

Afterwards, I needed to create notifications from inside a custom-Cell (i.e. when the user touches a cell, the gestureRecognizer of the custom-cell fires a local-Notification (with the cell tag-Nr as a message passed) - that again, makes the tableView.updates()

See notification methods at the very bottom of my code-excert....

Here is my code (excerts):

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // fixed to three to test...
    return 3
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = fromToTableView.dequeueReusableCell(withIdentifier: "FromToCell") as? FromToCustomTableViewCell

    cell?.configureCell(tag: indexPath.row)
    return cell!
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    if self.cellIsSelected == indexPath {
        return 450
    }
    return 144
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    self.cellIsSelected = self.cellIsSelected == indexPath ? nil : indexPath
    tableView.beginUpdates()
    tableView.endUpdates()
}

// MARK: Notifications methods

@objc func expandCellHeightFromNotification(_ notification: NSNotification) {
    if let tag = notification.userInfo?["tag"] as? Int {
        self.cellIsSelected = IndexPath(item: tag, section: 0)
    }
    self.fromToTableView.beginUpdates()
    self.fromToTableView.endUpdates()
}

@objc func collapseCellHeightFromNotification(_ notification: NSNotification) {
    self.cellIsSelected = nil
    self.fromToTableView.beginUpdates()
    self.fromToTableView.endUpdates()
}
iKK
  • 6,394
  • 10
  • 58
  • 131