Ios 在单元格中隐藏按钮

Ios 在单元格中隐藏按钮,ios,swift2,Ios,Swift2,我正在开发一个应用程序,用户可以在一个单元格中选择两张图片中的一张。我的原型细胞看起来像: 我有 cell.rightVoteButton.addTarget(self, action: #selector(voteRightButtonPressed), forControlEvents: .TouchUpInside) cell.leftVoteButton.addTarget(self, action: #selector(voteLeftButtonPressed), forCont

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

我有

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
    }

}

我假设您有一个具有多个此类单元格的tableview,如果是这种情况,您需要一个custonTableViewCell来放置此代码。按钮响应一个@iAction,该@iAction隐藏按钮并调用其UITableViewController中的代理

了解您的custonTableViewCell,就像了解每个单元格的viewController一样

将其添加到custon单元格中,并指向按钮

//按钮的IBAction

@IBAction func buttonPress(sender: UIButton){
    leftButton.hidden = true
    rightImage.hidden = true
    self.delegate?.buttonPress(sender.tag)
}
将其添加到custon单元类定义之前

protocol CustomTableViewCellDelegate {
   func buttonPress(tag: Int)
}
这之后:

var delegate : CustomTableViewCellDelegate?
在tableviewcontroller中

class TableViewController: UITableViewController, CustomTableViewCellDelegate
还有你在某个地方的新功能

func buttonPress(tag: Int){
    print(tag)
}

我假设您有一个具有多个此类单元格的tableview,如果是这种情况,您需要一个custonTableViewCell来放置此代码。按钮响应一个@iAction,该@iAction隐藏按钮并调用其UITableViewController中的代理

了解您的custonTableViewCell,就像了解每个单元格的viewController一样

将其添加到custon单元格中,并指向按钮

//按钮的IBAction

@IBAction func buttonPress(sender: UIButton){
    leftButton.hidden = true
    rightImage.hidden = true
    self.delegate?.buttonPress(sender.tag)
}
将其添加到custon单元类定义之前

protocol CustomTableViewCellDelegate {
   func buttonPress(tag: Int)
}
这之后:

var delegate : CustomTableViewCellDelegate?
在tableviewcontroller中

class TableViewController: UITableViewController, CustomTableViewCellDelegate
还有你在某个地方的新功能

func buttonPress(tag: Int){
    print(tag)
}

不要使用标签,标签没有内在意义,容易混淆,你可以说:

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)

}

一旦你有了单元格,你就可以隐藏你想要的视图,并且你可以更新你的模型来反映你刚刚投下的票。

你可以说这样的话,而不是使用标签,因为标签没有内在的意义,很容易混淆:

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)

}

拥有单元格后,您可以隐藏所需的视图,并可以更新模型以反映刚刚投下的选票。

我在帖子中添加我的自定义TableViewCell我在帖子中添加我的自定义TableViewCell