在 iOS 开发中,当需要在 UITableViewCell 中的按钮被点击时显示一个带标签的弹出屏幕,这通常涉及到以下几个核心概念:
// 在自定义 UITableViewCell 类中
class CustomTableViewCell: UITableViewCell {
@IBOutlet weak var button: UIButton!
var indexPath: IndexPath?
@IBAction func buttonTapped(_ sender: UIButton) {
// 通过通知或闭包将事件传递到视图控制器
NotificationCenter.default.post(name: Notification.Name("ButtonTappedInCell"), object: nil, userInfo: ["indexPath": indexPath!])
}
}
// 在包含 UITableView 的视图控制器中
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
NotificationCenter.default.addObserver(self, selector: #selector(handleButtonTap(notification:)), name: Notification.Name("ButtonTappedInCell"), object: nil)
}
@objc func handleButtonTap(notification: Notification) {
guard let indexPath = notification.userInfo?["indexPath"] as? IndexPath else { return }
// 创建带标签的弹窗
let alert = UIAlertController(title: "提示", message: "您点击了第\(indexPath.row)行的按钮", preferredStyle: .alert)
// 添加标签(在UIAlertController中,message属性已经相当于标签)
alert.addAction(UIAlertAction(title: "确定", style: .default, handler: nil))
present(alert, animated: true, completion: nil)
}
// UITableViewDataSource 方法
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomTableViewCell
cell.indexPath = indexPath
return cell
}
// 其他必要的方法...
}
// 自定义弹出视图
class CustomPopupView: UIView {
let label: UILabel = {
let label = UILabel()
label.textAlignment = .center
label.numberOfLines = 0
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
init(frame: CGRect, text: String) {
super.init(frame: frame)
backgroundColor = .white
layer.cornerRadius = 10
layer.shadowOpacity = 0.3
layer.shadowRadius = 5
label.text = text
addSubview(label)
NSLayoutConstraint.activate([
label.centerXAnchor.constraint(equalTo: centerXAnchor),
label.centerYAnchor.constraint(equalTo: centerYAnchor),
label.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20),
label.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -20)
])
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
// 在视图控制器中修改 handleButtonTap 方法
@objc func handleButtonTap(notification: Notification) {
guard let indexPath = notification.userInfo?["indexPath"] as? IndexPath else { return }
let popupView = CustomPopupView(frame: CGRect(x: 0, y: 0, width: 250, height: 150), text: "您点击了第\(indexPath.row)行的按钮")
popupView.center = view.center
// 添加背景遮罩
let backgroundView = UIView(frame: view.bounds)
backgroundView.backgroundColor = UIColor.black.withAlphaComponent(0.5)
backgroundView.tag = 999
backgroundView.addSubview(popupView)
view.addSubview(backgroundView)
// 添加点击手势关闭弹窗
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(dismissPopup))
backgroundView.addGestureRecognizer(tapGesture)
}
@objc func dismissPopup() {
view.viewWithTag(999)?.removeFromSuperview()
}
问题1:按钮点击事件无法触发
问题2:弹出视图位置不正确
问题3:内存泄漏
问题4:弹出视图被遮挡
以上方案可以根据实际需求选择使用,系统弹窗适合简单场景,自定义视图则提供更大的灵活性。
没有搜到相关的文章