简体   繁体   English

如何快速在 UITableViewCell Swipe 上添加自定义 UIView?

[英]How to add Custom UIView on UITableViewCell Swipe in swift?

I want to add custom UIView on UITableViewCell Swipe.我想在UITableViewCell Swipe 上添加自定义 UIView。 Basically I want to add two UIButton , one on top and other just below it.基本上我想添加两个UIButton ,一个在上面,另一个在它下面。 Both having height half of cell.两者都具有单元格的一半高度。 If I use editActionsForRowAtIndexPath delegate method of UITableView that this add UIButton side by side but i want to add buttons on top having height equal to half of table view cell and other just below it having same height.如果我使用UITableView editActionsForRowAtIndexPath委托方法,这将并排添加UIButton但我想在顶部添加按钮,其高度等于表格视图单元格的一半,而其他按钮在其下方具有相同的高度。

Thanks in advance!!!提前致谢!!!

you can add this code in your custom tableview cell:您可以在自定义 tableview 单元格中添加此代码:

var topBtn = UIButton()
var behindBtn = UIButton()

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    awakeFromNib()
}

override func awakeFromNib() {
    topBtn.setTitle("top", for: .normal)
    topBtn.backgroundColor = UIColor.blue
    addSubview(topBtn)

    behindBtn.setTitle("behind", for: .normal)
    behindBtn.backgroundColor = UIColor.green
    addSubview(behindBtn)
}

override func layoutSubviews() {
    super.layoutSubviews()
    let btnH = self.frame.size.height/2
    let btnW = self.frame.size.width

    topBtn.frame = CGRect(x: 0, y: 0, width: btnW, height: btnH)
    behindBtn.frame = CGRect(x: 0, y: btnH, width: btnW, height: btnH)
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

and this is the result:这是结果: 在此处输入图片说明

You want to add a view, which contains two buttons and appears on swipe event?您想添加一个包含两个按钮并出现在滑动事件上的视图? If I clearly got a question you can add this view on UITableViewCell, hide it and set hidden = NO on swipe event.如果我清楚地有问题,您可以在 UITableViewCell 上添加此视图,将其隐藏并在滑动事件上设置 hidden = NO。 Also you may use UIPanGestureRecognizer and setting the views alpha.您也可以使用 UIPanGestureRecognizer 并设置视图 alpha。 Or change the frame, in this case would be better to set cell.clipsToBounds = YES或者改变框架,在这种情况下最好设置 cell.clipsToBounds = YES

You can start by implementing this method:你可以从实现这个方法开始:

    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    // you need to implement this method too or you can't swipe to display the actions
}

Then you can customize it by adding two buttons in a view as you want it to and returning that view in your editActionsForRowAtIndexPath method.然后,您可以根据需要在视图中添加两个按钮并在您的editActionsForRowAtIndexPath方法中返回该视图来自定义它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM