0

I have three Cells in TableView. It has a button on the first cell and when it is pressed, the vertical size of the first cell is expanded.

In ViewController :

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPath.section == 1 {
        return 0
    }
    else if indexPath.section == 0 {
        return CGFloat(expandedHeight) //700 //500
    }
    else if indexPath.section == 2 {
        return 240
    }
    return 133
} 
//I've modified some of my code, but it works this way.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.section == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "one", for: indexPath) as! one

        return cell
    }
    else if indexPath.section == 1 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "two", for: indexPath) as! two

        return cell
    }
    else if indexPath.section == 2 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "thr", for: indexPath) as! thr

        return cell
    }

    return UITableViewCell()
}

func ExpandedButtonClick(_ height: Int)
{
    tableView.beginUpdates()
    expandedHeight = height // 500 700 switch
    tableView.endUpdates()
}

In TableViewCell (one) :

@IBAction func btnClickExpanded(_ sender: Any)
{
    if let myViewController = parentViewController as? ViewController
    {
        if constraintExpend.constant == 500
        {
            constraintExpend.constant = 700
            myViewController.ExpandedButtonClick(700)
        }
        else
        {
            constraintExpend.constant = 500
            myViewController.ExpandedButtonClick(500)
        }
    }
}

Everything worked perfectly, but there is a problem.

Only the parts of the screen that I see should be animated, but the whole screen is moving. It is disturbing to see this.

What if I want to animate only the expanded part?

I want to expand while holding the screen fixed.

Enkha
  • 430
  • 1
  • 6
  • 23
  • ___but the whole screen is moving___ what does that actually mean? Can you show a video? – nayem Dec 11 '18 at 08:54
  • When the scroll is in the middle, pressing the button moves the scroll to the top and then back to the middle. – Enkha Dec 11 '18 at 08:55
  • `beginUpdates()` and `endUpdates()` for *subsequent insertions, deletion, and selection operations* [from documentation](https://developer.apple.com/documentation/uikit/uitableview/1614908-beginupdates) – vpoltave Dec 11 '18 at 08:59
  • Can you show how you defined the height for your cell? – nayem Dec 11 '18 at 09:04
  • @Vitaliy Poltavets I`ll try it @nayem I added the code! – Enkha Dec 11 '18 at 09:08
  • Make sure you have implemented `estimatedHeightForRowAt`. The value doesn't have to be exactly right, but reasonably close. 500 would probably be a good value for section 0 in your case – Paulw11 Dec 11 '18 at 09:39

1 Answers1

0

I had difficulty finding it because the question was a little different.

In conclusion, I found the perfect answer.

@Sujan Simha reloadData() of UITableView with Dynamic cell heights causes jumpy scrolling

In my question, the ExpandedButtonClick function

func ExpandedButtonClick (_ height: Int)
{
    expandedHeight = height
    tableView.beginUpdates ()
    tableView.endUpdates ()
    tableView.layer.removeAllAnimations ()
    tableView.setContentOffset (tableView.contentOffset, animated: false)
}

This works perfectly!

Enkha
  • 430
  • 1
  • 6
  • 23