1

I am trying to create a table view with expandable cells. The cells also have an offset from both sides by a few pixels and a shadowPath set as well.

Example gif which I am referring to.

The first problem is that the shadowPaths are not getting animated properly while the cells are reloading its height and position. It is an instant jump from no shadow to full shadow after the height/position animation stops. (pay attention to the tapped cell)

Second problem occurs when you tap on another cell while having one already expanded. The shadowPath of the first expanded cell jumps to the normal height of the cell instantly while the cell is still animating the reduced height. (pay attention to the first expanded cell)

The third bug is not happening all the time and also isn't related to the shadows (I suppose?) but I've managed to capture it while recording the gif. When I've tapped the first time while none of the cells were expanded one cell went right under the cell above. (look at the two bottom cells)

Anyways here's the code for the custom tableViewCell:

override func awakeFromNib() {
    super.awakeFromNib()
    self.clipsToBounds = false
    self.layer.shadowOffset = CGSizeMake(1, 1)
    self.layer.shadowOpacity = 0.9
    self.layer.shadowColor = UIColor.blackColor().CGColor
    self.layer.shadowRadius = 2
    self.selectionStyle = .None
}

override var frame: CGRect {
    get {
        return super.frame
    }
    set {
        var tempFrame = newValue
        let inset: CGFloat = cellOffset
        tempFrame.origin.x += inset
        if previousFrame != tempFrame.size.width {
            tempFrame.size.width -= 2 * inset
            previousFrame = tempFrame.size.width
        }
        super.frame = tempFrame
    }
}

override func layoutSubviews() {
    super.layoutSubviews()
    let shPath: CGPathRef = UIBezierPath(rect: self.bounds).CGPath
    self.layer.shadowPath = shPath
}

TableView related code:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if let tempIndexPath = selectedCellIndexPath where tempIndexPath == indexPath {
        selectedCellIndexPath = nil
    } else {
        selectedCellIndexPath = indexPath
    }
    tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)

    tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true)
}


func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if selectedCellIndexPath == indexPath {
        return 200
    }
    return 80
}

I will try to respond asap to any answer/question regarding the topic. Thank you.

David
  • 2,109
  • 1
  • 22
  • 27

0 Answers0