【问题标题】:custom UITableViewCell button isn't executing its function自定义 UITableViewCell 按钮未执行其功能
【发布时间】:2021-05-17 08:42:41
【问题描述】:

我的视图控制器中有一个带有自定义单元格的表格视图。在单元格中,我有一个应该做某事的功能。我有一个正在执行的打印语句。

@objc func cellButtonTapped() {
    cellText.backgroundColor = .red
    print("cell button tapped")
}

当我点击按钮时,我确实在控制台中看到了打印语句,但它并没有像我想要的那样改变单元格背景颜色。

这是tableViewCell中的cellText

let cellText : UITextField = {
    let cellText = UITextField()
    cellText.isUserInteractionEnabled = false
    return cellText
}()

这是它在单元格中的布局方式。我不认为它的布局很重要

override func layoutSubviews() {
    super.layoutSubviews()
    contentView.addSubview(cellText)
    contentView.addSubview(cellButton)
    cellText.frame = CGRect(x: 0,
                                 y: 0,
                                 width: contentView.width - 30,
                                 height: contentView.height)
    cellButton.frame = CGRect(x: cellText.right, y: 0, width: 30, height: contentView.height)
    
    

}

【问题讨论】:

  • 什么是cellText
  • 请分享完整的上下文
  • 我更新了信息。希望这能给你更多的背景

标签: ios swift uitableview tableview viewcontroller


【解决方案1】:

我必须做的是在 viewController 中创建一个具有单元格所需值的变量。就我而言,我希望结转一个布尔值。

var isBool : Bool = false 

然后我让函数为按钮改变变量值:

@objc func sendButtonTapped() {
    isBool = !isBool
    tableView.reloadData()
    print("send button tapped")
}

这里的函数每次点击按钮时都会改变我的布尔值。

我将值转移到 tableview 中带有 cellForRowAt 的单元格。

【讨论】:

    猜你喜欢
    • 2013-05-01
    • 2015-01-08
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    • 2011-11-15
    相关资源
    最近更新 更多