I have a TableView with expandable cells on tap, using this code:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let index = indexPath
if(selectedIndexPath != nil) {
if(index == selectedIndexPath) {
return 90
}
else {
return 60
}
}
else {
return 60
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch selectedIndexPath {
case nil:
selectedIndexPath = indexPath
default:
if selectedIndexPath! == indexPath {
selectedIndexPath = nil
}
else {
selectedIndexPath = indexPath
}
}
tableView.reloadRows(at: [indexPath], with: .automatic)
}
Now, I also want to add swipe gestures for every cell, using code founded here.
func tableView(tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let share = UITableViewRowAction(style: .Normal, title: "Share") { action, index in
print("share button tapped")
}
return [share]
}
Everything is good, except the height of the cells while swiping.
- cells swiped, sometimes, have big height (even they are not selected)
- cells swiped, sometimes, have small height (even they are selected).
I think it's maybe because of the last selected cell height.
I tried to fix with selectedIndexPath = indexPath
in editActionsForRowAt
but problem still exists.
https://i.stack.imgur.com/iQCEe.png
Edit: I noticied that if heightForRowAt
return always 60 the problem remains.