gpt4 book ai didi

ios - 在 Swift 中,如何翻转 UITableViewCell?

转载 作者:行者123 更新时间:2023-11-29 00:50:55 28 4
gpt4 key购买 nike

我有一个 tableView,当按下一个单元格时,我需要它翻转到另一个单元格。例如:

enter image description here

对此:enter image description here

一次一个单元格。我在这里看到了一篇关于如何在 objective-c 中做到这一点的帖子: How can I flip an iOS UITableViewCell?

有没有办法在 Swift 中做到这一点?

最佳答案

enter image description here

enter image description here

enter image description here

此处以防您需要披露指标。据我所知,您必须使用自定义的才能翻转它。我发现这个函数可以水平翻转单元格的 View ,但我认为这不是您想要的。

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

我只是让两个单元格都带有自定义披露指示符,您只需保存翻转的披露指示符图像的副本,然后当您单击单元格时让它们像这样交换:

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()
}
}

如果你想要那些家伙使用的代码,我只是把它跑进了 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
})
}

回复您的评论

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()
}
}

关于ios - 在 Swift 中,如何翻转 UITableViewCell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38150620/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com