SwiftyJD SwiftyJD - 5 months ago 48
Swift Question

In Swift, how to flip a UITableViewCell?

I have a tableView that when a cell is pressed I need it to flip to another cell. For example from this:

enter image description here

To this:enter image description here

One cell at a time. I saw a post on how to do it in objective-c here:
How can I flip an iOS UITableViewCell?

Is there a way to do it in Swift?

Answer

enter image description here

enter image description here

enter image description here

Here in case you need the disclosure indicators. You will have to use a custom one in order to flip it as far as I know. I found this function that will flip a cell's view horizontally, but I don't believe that is what you want.

cell.contentView.layer.setAffineTransform(CGAffineTransformMakeScale(-1, 1))

I would just make both cells with the custom disclosure indicators that you just save copies of the disclosure indicator images flipped, and then when you click the cell make them swap like so:

var isFirstCell = true
var indexOfSwappingCell: Int = 0

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    isFirstCell = !isFirstCell
    tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: indexOfSwappingCell, inSection: 0)], withRowAnimation: .None)
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if indexPath.row == indexOfSwappingCell {
        if isFirstCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("customCell1", forIndexPath: indexPath) as! CustomCell1
            //setup cell
            return cell
        } else {
            let cell = tableView.dequeueReusableCellWithIdentifier("customCell2", forIndexPath: indexPath) as! CustomCell2
            //setup cell
            return cell
        }
    } else {
        //setup other cells

        return UITableViewCell()
    }
}

In case you want the code that guys used, I just ran through it into objectivec2swift

//flip the view to flipView

@IBAction func flipButtonAction(sender: UIButton) {
    UIView.transitionWithView(self.contentView, duration: 0.6, options: .TransitionFlipFromRight, animations: {() -> Void in
        self.contentView.insertSubview(flipView, aboveSubview: normalView)
    }, completion: {(finished: Bool) -> Void in
    })
}
//flip the view back to normalView

@IBAction func flipBackButtonAction(sender: AnyObject) {
    UIView.transitionWithView(self.contentView, duration: 0.6, options: .TransitionFlipFromLeft, animations: {() -> Void in
        self.contentView.insertSubview(normalView, aboveSubview: flipView)
    }, completion: {(finished: Bool) -> Void in
    })
}

To respond to your comment

add these 2 above viewDidLoad

var isFirstLoad = true
var contentViewToFlip: UIView!

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if indexPath.row == indexOfSwappingCell {

        if isFirstCell {
            let cell = tableView.dequeueReusableCellWithIdentifier("customCell1", forIndexPath: indexPath) as! CustomCell1
            if isFirstLoad == false {
                UIView.transitionWithView(self.contentView, duration: 0.6, options: .TransitionFlipFromRight, animations: {() -> Void in
                    contentViewToFlip.insertSubview(cell.contentView, aboveSubview: contentViewToFlip)
                    }, completion: {(finished: Bool) -> Void in
                })
            }
            isFirstLoad = false
            contentViewToFlip = cell.contentView
            //setup cell
            return cell
        } else {
            let cell = tableView.dequeueReusableCellWithIdentifier("customCell2", forIndexPath: indexPath) as! CustomCell2
            UIView.transitionWithView(self.contentView, duration: 0.6, options: .TransitionFlipFromLeft, animations: {() -> Void in
                contentViewToFlip.insertSubview(cell.contentView, aboveSubview: contentViewToFlip)
                }, completion: {(finished: Bool) -> Void in
            })
            contentViewToFlip = cell.contentView
            //setup cell
            return cell
        }
    } else {
        //setup other cells

        return UITableViewCell()
    }
}