ios - 在单元格中隐藏按钮

标签 ios swift2

我正在开发一个应用程序,用户可以在一个单元格中选择两张图片中的一张。我的原型(prototype)单元看起来像:

enter image description here

我有

cell.rightVoteButton.addTarget(self, action: #selector(voteRightButtonPressed), forControlEvents: .TouchUpInside)

cell.leftVoteButton.addTarget(self, action: #selector(voteLeftButtonPressed), forControlEvents: .TouchUpInside)

tableView 函数中。

func voteRightButtonPressed(sender:UIButton){

    print("Right Vote clicked is \(sender.tag)")
    print(self.polls[sender.tag].poll_id)

}

通过这种方式,我打印了民意测验 ID 和单元格索引。

我想在单击特定单元格后隐藏投票按钮。 例如,如果我单击第一个单元格和左侧图片中的投票,我想隐藏第一个单元格上的两个按钮。我现在是单元格的索引,但如何隐藏特定单元格中的按钮。

我的自定义 TableViewCell:

class CustomTableViewCell: UITableViewCell {

    @IBOutlet weak var leftImage: UIImageView!
    @IBOutlet weak var rightImage: UIImageView!
    @IBOutlet weak var userPicture: UIImageView!
    @IBOutlet weak var userName: UILabel!
    @IBOutlet weak var leftButton: UIButton!
    @IBOutlet weak var rightButton: UIButton!



    @IBOutlet weak var pollDescription: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

最佳答案

与其使用没有内在含义且容易混淆的标签,不如这样说:

func voteRightButtonPressed(sender:UIButton){
    let location = self.tableView.convertPoint(sender.bounds.origin, fromView:sender)
    let indexPath = self.tableView.indexPathForRowAtPoint(location)
    if let cell = self.tableView.cellForRowAtIndexPath(indexPath) as? CustomUITableViewCell {

    //hide views in cell and update your model to reflect vote.
    }
    print("Right Vote clicked is \(indexPath.row)")
    print(self.polls[indexPath.row].poll_id)

}

一旦你有了单元格,你就可以隐藏你想要的 View ,你可以更新你的模型来反射(reflect)刚刚投的票。

关于ios - 在单元格中隐藏按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36754362/

相关文章:

ios - 源代码控制 xCode 6 和现有文件

ios - 如何在出现键盘时对 UIView 进行排序?

ios - swift 2 : "the error is not handled"

ios - 如何在 Swift 2 中切换到前置摄像头?

带有对象参数的特定 UITextfield 的 Swift 2 addObserver

android - 消息 : Support Emijo between Android and iOS

ios - 核心数据重复

ios - 我已经尝试了所有方法,但无法将 Swift 文件导入到 Objective-C 文件

ios - 如何从 NSDictionary 获取 IndexPath

Swift Playground - 实例成员不能在类型自定义类上使用