ios - UITableViewCell 内的 UIView 转换不起作用

标签 ios iphone swift uitableview uiview

我在 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/

相关文章:

ios - Xamarin iOS - 处理已清除的通知

iphone - 亲自注册 iPhone 开发者计划吗?

swift - Alamofire SSL 失败

ios - 在 ios 中的 sqlite 更新中出现错误

php - 如何国际化/本地化您的 FCM 推送通知,尤其是主题?

iphone - iOS 4.2 不允许 MKMapView 在另一个线程中初始化

ios - 如何更改已经具有高度 anchor 的 UIView 的高度?

ios - Swinject 容器注册失败

ios - Cordova 应用程序的代码签名失败(使用 XCode 5.1.1 和 Cordova 3.4.1-0.1.0)

iphone - 具有 ASPN、应用内购买等的多个应用程序中的应用程序 ID 和捆绑种子 ID