skip to Main Content

I have expandable table view cells, everything works, except animation. I have one cell with UISwitch, when I tap on it, other cells appear but with some view movements, same thing when I hide these cells when I tap on UISwitch.

I’m using insetGroup Table View and these movements make view square instead of round.

I want to keep animation, I tried reloadSections(IndexSet(integer: 0), with: .fade) seems like a solution, but it also reloads my header, but I don’t want that.

My cellForRowAt func:

internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "RemindersCell", for: indexPath) as! RemindersCell
    
    cell.switchReminder.isOn = remindersAllowed ? true : false
    cell.switchReminder.addTarget(self, action: #selector(switchTapped(sender:)), for: .touchUpInside)
    
    return cell
}

My numberOfRowsInSection func:

internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return remindersAllowed ? 5 : 1
}

My func for UISwitch:

@objc private func switchTapped(sender: UISwitch) {
    
    if !remindersAllowed {
        // Add
        remindersAllowed = true
    } else {
        // Delete
        remindersAllowed = false
    }

    tableView.beginUpdates()
    tableView.reloadSections(IndexSet(integer: 0), with: .none)
    tableView.endUpdates()
}

Default remindersAllowed is true, when I switch it becomes false and hides cells. I really don’t understand what the problem is, any help would be appreciated!

The gif shows this bug when I hide the cells.

Here when I hide cells you can notice

2

Answers


  1. you can use activate cell as a tableviewheader

    Login or Signup to reply.
  2. You can try this code to delete system reload section animation and reload section with fade animation:

    tableView.reloadData()
    tableView.reloadSections(IndexSet(integer: 0), with: .fade)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search