3

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.

  1. cells swiped, sometimes, have big height (even they are not selected)
  2. 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.

Community
  • 1
  • 1
Johnatan
  • 31
  • 3

3 Answers3

0
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
   let index = indexPath

    if(selectedIndexPath != nil) {
        if(index == selectedIndexPath) {
            return 90
        }
        else {
            return 60
        }
    }
    else {
        return 60
    }
}

its work for me or u can also check if any tableview cell all constraint proper. or u can use only one any of tableview height method to obtain better result.

0

I believe that your input is accidentally getting selected/deselected when you enter editing mode for the cells, due to either the initial tap on the cell or some additional gesture made by the user when you try to access the editing actions.

To avoid this, set tableView.allowsSelectionDuringEditing = false in your viewDidLoad.

Alternatively, try wrapping your code in didSelectRowAt with if tableView.editing == false.

Kevin Aleman
  • 393
  • 3
  • 9
0

I solved by initially hiding the content (in my case is one button named button) showed after tapping (View > Drawing > check Hidden) and in cellForRowAt I use:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cellIdentifier = "CustomCell"
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath as IndexPath) as! CustomCell
    if(selectedIndexPath != nil) {
        if(indexPath == selectedIndexPath) {
            cell.button.isHidden = false
        }
    }
    return cell
}
Johnatan
  • 31
  • 3