我正在嘗試創建一個應用程式,但我遇到了一個簡單的問題:如何向我的自定義單元格添加洗掉按鈕?
我創建了一個 NewCocoaTouchClass
我已經創建了我的自定義設計(還為每個單元格添加了一個按鈕)并在 TableViewCellController 中提供了 Outlets
我設法將我的自定義單元實作到另一個 viewController
我在 viewController 上創建了一個按鈕
我希望當用戶點擊我的新按鈕時,會出現單元格按鈕,然后我可以單獨洗掉單元格。當我創建 IbAction 時,我無法檢索單元格代碼,因為該單元格僅在我的 tableViewCode 中定義。
預先感謝
Ps:我也實作了通過滑動洗掉單元格,但我的自定義單元格設計與標準矩形洗掉選項不兼容(這很糟糕)。
表視圖單元控制器
import UIKit
class CustomTableTableViewCell: UITableViewCell {
static let identificatore = "CustomTableTableViewCell"
static func nib() -> UINib {
return UINib(nibName: "CustomTableTableViewCell", bundle: nil)
}
@IBOutlet weak var ImmagineCella: UIImageView!
@IBOutlet weak var TestoCella: UILabel!
@IBOutlet weak var Button: UIButton!
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
}
}
視圖控制器
import UIKit
class MaterieViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var materie : [String] = ["zoifvdhfdv", "szvzv", "zdvzfv", "zfdvbfdb", "bfzdfb"]
@IBOutlet weak var TableViewMaterie: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
TableViewMaterie.register(CustomTableTableViewCell.nib(), forCellReuseIdentifier: CustomTableTableViewCell.identificatore)
TableViewMaterie.delegate = self
TableViewMaterie.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return materie.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: CustomTableTableViewCell.identificatore, for: indexPath) as! CustomTableTableViewCell
cell.TestoCella.text = materie[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 67.83
}
@IBAction func EditButton(_ sender: UIButton) {
}
}
uj5u.com熱心網友回復:
這是我在專案中使用的代碼
func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String?
{
return "Delete"
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if (editingStyle == .delete) {
//Add your code
}
}
uj5u.com熱心網友回復:
你可以寫添加目標
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: CustomTableTableViewCell.identificatore, for: indexPath) as! CustomTableTableViewCell
cell.TestoCella.text = materie[indexPath.row]
cell. Button.tag = indexPath.row
cell. Button.addTarget(self, action: #selector(buttonClicked(sender:)), for: .touchUpInside)
return cell
}
func buttonClicked(sender: UIButton) {
let buttonRow = sender.tag
print(buttonRow)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/395127.html
上一篇:委托后如何為網點賦值?
