我有麻烦从表中删除细胞,我试图创建一些协议,但它没有工作,因为我新的这种语言,我想不出很多解决方案。
extension CustomTableViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return sneakers.count + 1
}
func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == sneakers.count {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "OrderSummaryView") as? OrderSummaryView else {
print("I'll never print")
return OrderSummaryView()
}
return cell
}
let product = sneakers[indexPath.row]
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ExampleCell") as? ExampleCell else {
print("I'll never print")
return ExampleCell()
}
print("Utilize cells")
cell.configure(product: product)
return cell
}
}
这是我如何使用我的表格单元格
import UIKit
final class ExampleCell: UITableViewCell {
private let productImageView: UIImageView = .init(frame: .zero)
private let categoryLabel: UILabel = .init(frame: .zero)
private let nameLabel: UILabel = .init(frame: .zero)
private let priceLabel: UILabel = .init(frame: .zero)
private let sizeLabel: UILabel = .init(frame: .zero)
private let colorLabel: UILabel = .init(frame: .zero)
private let removeButton = UIButton()
// Replace "transparent" with the name of your transparent image
private let clearImage = UIImage(named: "Image-1")
let qtyButton = UIButton()
private let arrowQty = UIImage(named: "Arrow_Qty")
private let wishButton = UIButton()
override init(style: UITableViewCell.CellStyle,reuseIdentifier: String?) {
super.init(style: .default,reuseIdentifier: "ExampleCell")
print("New cell has been create")
setupUI()
setupConstraints()
}
// Init with coder is when the cell is instaciated via storyboard/XIB
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func configure(product: Product) {
productImageView.image = product.image
categoryLabel.text = product.category
nameLabel.text = product.name
priceLabel.text = "\(product.price)€"
sizeLabel.text = "Size: \(product.size)"
colorLabel.text = "Color: \(product.color)"
}
private func setupUI() {
selectionStyle = .none
let array = [productImageView,
categoryLabel,
nameLabel,
priceLabel,
sizeLabel,
colorLabel,
removeButton,
qtyButton,
wishButton]
array.forEach { view in
view.translatesAutoresizingMaskIntoConstraints = false
addSubview(view)
}
contentView.translatesAutoresizingMaskIntoConstraints = false
productImageView.layer.cornerRadius = 16
categoryLabel.font = FontStyle.uiS.baseUIFont
categoryLabel.textColor = UIColor(named: "GreyScale_400")
nameLabel.font = FontStyle.displayMBold.baseUIFont
nameLabel.textColor = UIColor(named: "GreyScale_700")
priceLabel.font = FontStyle.uiS.baseUIFont
priceLabel.textColor = UIColor(named: "GreyScale_700")
sizeLabel.font = FontStyle.uiXS.baseUIFont
sizeLabel.textColor = UIColor(named: "GreyScale_700")
colorLabel.font = FontStyle.uiXS.baseUIFont
colorLabel.textColor = UIColor(named: "GreyScale_700")
removeButton.setImage(clearImage, for: .normal)
qtyButton.setTitle("Qty:1 ", for: .normal)
qtyButton.setImage(arrowQty, for: .normal)
qtyButton.semanticContentAttribute = .forceRightToLeft
qtyButton.layer.cornerRadius = 16
qtyButton.backgroundColor = UIColor(named: "GreyScale_White")
qtyButton.setTitleColor(UIColor(named: "GreyScale_700"), for: .normal)
qtyButton.layer.borderWidth = 1
qtyButton.layer.borderColor = UIColor(named: "GreyScale_400")?.cgColor
qtyButton.titleLabel?.font = FontStyle.uiM.baseUIFont
wishButton.setTitle("Move to wishlist", for: .normal)
wishButton.setTitleColor(UIColor(named: "GreyScale_700"), for: .normal)
wishButton.titleLabel?.font = FontStyle.uiM.baseUIFont
//underline wishButton
let attributedTitle = NSAttributedString(string: "Move to wishlist", attributes: [.underlineStyle: NSUnderlineStyle.single.rawValue])
wishButton.setAttributedTitle(attributedTitle, for: .normal)
}
private func setupConstraints() {
NSLayoutConstraint.activate([
contentView.widthAnchor.constraint(equalToConstant: 350),
contentView.heightAnchor.constraint(equalToConstant: 242),
productImageView.topAnchor.constraint(equalTo: contentView.topAnchor),
productImageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 20),
productImageView.widthAnchor.constraint(equalToConstant: 106),
productImageView.heightAnchor.constraint(equalToConstant: 114),
categoryLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 18),
categoryLabel.leadingAnchor.constraint(equalTo: productImageView.trailingAnchor, constant: 32),
categoryLabel.widthAnchor.constraint(equalToConstant: 29),
categoryLabel.heightAnchor.constraint(equalToConstant: 14),
nameLabel.topAnchor.constraint(equalTo: categoryLabel.bottomAnchor, constant: 8),
nameLabel.leadingAnchor.constraint(equalTo: productImageView.trailingAnchor, constant: 32),
nameLabel.widthAnchor.constraint(equalToConstant: 159),
nameLabel.heightAnchor.constraint(equalToConstant: 24),
priceLabel.topAnchor.constraint(equalTo: nameLabel.bottomAnchor, constant: 8),
priceLabel.leadingAnchor.constraint(equalTo: productImageView.trailingAnchor, constant: 32),
priceLabel.widthAnchor.constraint(equalToConstant: 56),
priceLabel.heightAnchor.constraint(equalToConstant: 14),
sizeLabel.topAnchor.constraint(equalTo: priceLabel.bottomAnchor, constant: 16),
sizeLabel.leadingAnchor.constraint(equalTo: productImageView.trailingAnchor, constant: 32),
sizeLabel.widthAnchor.constraint(equalToConstant: 46),
sizeLabel.heightAnchor.constraint(equalToConstant: 12),
colorLabel.topAnchor.constraint(equalTo: priceLabel.bottomAnchor, constant: 16),
colorLabel.leadingAnchor.constraint(equalTo: sizeLabel.trailingAnchor, constant: 16),
colorLabel.widthAnchor.constraint(equalToConstant: 62),
colorLabel.heightAnchor.constraint(equalToConstant: 12),
removeButton.leadingAnchor.constraint(equalTo: contentView.trailingAnchor),
removeButton.widthAnchor.constraint(equalToConstant: 12),
removeButton.heightAnchor.constraint(equalToConstant: 12),
qtyButton.topAnchor.constraint(equalTo: productImageView.bottomAnchor, constant: 32),
qtyButton.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 32),
qtyButton.widthAnchor.constraint(equalToConstant: 106),
qtyButton.leadingAnchor.constraint(equalTo: contentView.leadingAnchor,constant: 20),
qtyButton.heightAnchor.constraint(equalToConstant: 64),
wishButton.topAnchor.constraint(equalTo: colorLabel.bottomAnchor, constant: 56),
wishButton.leadingAnchor.constraint(equalTo: qtyButton.trailingAnchor, constant: 128),
wishButton.widthAnchor.constraint(equalToConstant: 116),
wishButton.heightAnchor.constraint(equalToConstant: 16),
])
}
}
这是我声明的东西里面的单元格与此我想删除按钮删除单元格从表和视图,你们能帮我吗?
1条答案
按热度按时间mnemlml81#
您希望在单元格中添加一个按钮操作,然后添加一个闭包,这样单元格就可以告诉表格视图控制器该按钮被点击了。
所以,在
ExampleCell
中...添加闭合:
同样在
ExampleCell
中-在你“设置UI”之后,为你的按钮添加一个动作:选择器Func可以看起来像这样:
现在,在控制器中,当您将
cellForRowAt
中的单元格出队时,请设置闭包:顺便说一句--在单元格出队时不要使用
guard let ...
:如果您错误地注册了表格视图单元格,您***希望***应用程序崩溃,以便您可以修复该问题。