簡體   English   中英

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

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

我想在UITableViewCell Swipe 上添加自定義 UIView。 基本上我想添加兩個UIButton ,一個在上面,另一個在它下面。 兩者都具有單元格的一半高度。 如果我使用UITableView editActionsForRowAtIndexPath委托方法,這將並排添加UIButton但我想在頂部添加按鈕,其高度等於表格視圖單元格的一半,而其他按鈕在其下方具有相同的高度。

提前致謝!!!

您可以在自定義 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")
}

這是結果: 在此處輸入圖片說明

您想添加一個包含兩個按鈕並出現在滑動事件上的視圖? 如果我清楚地有問題,您可以在 UITableViewCell 上添加此視圖,將其隱藏並在滑動事件上設置 hidden = NO。 您也可以使用 UIPanGestureRecognizer 並設置視圖 alpha。 或者改變框架,在這種情況下最好設置 cell.clipsToBounds = YES

你可以從實現這個方法開始:

    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
}

然后,您可以根據需要在視圖中添加兩個按鈕並在您的editActionsForRowAtIndexPath方法中返回該視圖來自定義它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM