我在 UITableViewCell 中有一个向上箭头按钮,当单击或在其他地方调用该方法时,该按钮应该旋转 180 度。当您单击按钮并更新 sender.transform 时,这实际上有效。
@IBAction func rotate(sender: UIButton) {
if (top)
{
UIView.animateWithDuration(0.5, animations: {
sender.transform = CGAffineTransformMakeRotation((360.0 * CGFloat(M_PI)) / 180.0)
})
}
else
{
UIView.animateWithDuration(0.5, animations: {
sender.transform = CGAffineTransformMakeRotation((180.0 * CGFloat(M_PI)) / 180.0)
})
}
}
如果我更改代码以便将按钮从 UITableViewCell 引用为类似的内容,则这不起作用。按钮不旋转。
IBAction func rotate(sender: UIButton) {
let detailCell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0)) as! DetailCell
if (top)
{
UIView.animateWithDuration(0.5, animations: {
detailCell.arrowButton.transform = CGAffineTransformMakeRotation((360.0 * CGFloat(M_PI)) / 180.0)
})
}
else
{
UIView.animateWithDuration(0.5, animations: {
detailCell.arrowButton.transform = CGAffineTransformMakeRotation((180.0 * CGFloat(M_PI)) / 180.0)
})
}
}
基本上,我希望能够以另一种不涉及单击按钮的方法从 UITableViewCell 旋转按钮,因此我需要能够从 UITableViewCell 引用按钮并旋转它。这样做的正确方法是什么?
最佳答案
您有几个不同的选择。
如果您创建 UITableViewCell
的自定义子类,那么您可以向单元格添加一个按钮导出。向 TableView 请求单元格,将其转换为自定义类,然后使用按钮的导出 (myCustomCell.button
)。
另一个选项是向按钮添加唯一标记,然后使用 cell.contentView.viewWithTag(tagNumber)
。然后将结果从 UIView 转换到 UIButton。
使用 UITableViewCell
的自定义子类和 socket 更干净且不易损坏,但需要进行更多设置。
关于ios - UITableViewCell 内的 UIView 转换不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30819990/