简体   繁体   English

如何更改重复单元中点击的按钮图像?

[英]How to change button image on tapped in repeating cell?

I'm doing todolist app and I need action for button, which on tapped will do change image in a cell which I tapped on it. 我正在做待办事项应用程序,我需要对按钮进行操作,点击该按钮将更改我点击的单元格中的图像。 Cell retrieves data from Firebase to UITableView. 单元从Firebase检索数据到UITableView。 I used button and image outlets and both reported this error: 我使用了按钮和图像插座,并且都报告了此错误:

The button from the TaskViewController to the UIButton is invalid. 从TaskViewController到UIButton的按钮无效。 Outlets cannot be connected to repeating content. 插座不能连接到重复内容。

I have this code for retrieves data to cell: 我有这段代码用于检索数据到单元格:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "TasksCell", for: indexPath) as! TasksCell

    let arrayTasks = tasks[indexPath.row]

    cell.taskName.text = arrayTasks.content

    return cell
}

and I have this in Storyboard 我在情节提要中有这个

在此处输入图片说明

object left of the label is button. 标签左侧的对象是button。

The button picture is set in storyboard. 按钮图片设置在情节提要中。

I don't know how to do it, because I don't have much experience. 我不知道该怎么做,因为我经验不足。 Please help me. 请帮我。

Try: 尝试:

  1. Open the Identity Inspector. 打开身份检查器。
  2. Click the prototype cell. 单击原型单元。
  3. Set the Class to TasksCell (in my example, it is WorkTableViewCell ). Class设置为TasksCell (在我的示例中为WorkTableViewCell )。
  4. Click in the Module field, empty it, and press enter. 在“ Module字段中单击,将其清空,然后按Enter。 It should change to Current - project_name_here (for example, Current - Artistry . 它应该更改为Current - project_name_here (例如, Current - Artistry

Here's a screenshot of what it should look like. 这是它的外观截图。 Check it to make sure you selected the right items in the right order: 检查它以确保您以正确的顺序选择了正确的项目:

说明图片

Then, delete the old outlet(s) to the cell (button, label, everything) from the Connections Inspector. 然后,从连接检查器中删除单元的旧插座(按钮,标签,所有内容)。 Do that step while selecting the cell, and then while selecting the view controller (but while selecting the view controller, only delete the outlet to the "completed" button). 在选择单元格时,然后在选择视图控制器时执行该步骤(但在选择视图控制器时, 删除“完成”按钮的插座)。

删除旧的出口

And finally, re-add them by opening the code (in a new window) for your TasksCell class, not the TaskViewController class (probably in TasksCell.swift ), and directly under the line : 最后,通过打开TasksCell的代码(在新窗口中)而不是 TaskViewController类(可能在TasksCell.swift ),并直接在下面的行中添加代码

class TasksCell: UITableViewCell {

(or similar), hold control and drag the button, label, etc. as shown: (或类似),按住控件并拖动按钮,标签等,如下所示:

建立新的店铺

And finally, finish creating the new outlets only from the label, button, etc. to the TasksCell class as mentioned earlier (see note below before pressing "connect"): 最后,完成仅从标签,按钮 等到TasksCell类的新插座的创建( 如前所述) (按“连接”之前,请参阅下面的注释):

新店

Change the Name as necessary. 根据需要更改Name It could be something like taskName for the label, or completedButton for the button. 标签可以是taskName ,按钮可以是completedButton Do not change the Type or any other fields. 请勿更改“ Type或任何其他字段。 Press "connect", and the code from your question should work. 按“连接”,问题代码便可以正常工作。

In your TasksCell class, connect the button to the cell, and you can access it easier (yeah agree to @ingraham, you can't connect it to your view controller): 在TasksCell类中,将按钮连接到单元格,您可以更轻松地访问它(是的,同意@ingraham,您不能将其连接到视图控制器):

class TasksCell: UITableViewCell {

    @IBOutlet weak var buttonCheckbox: UIButton!
    @IBOutlet weak var taskName: UILabel!
    @IBOutlet weak var updatedDate: UILabel!

}

You can use image view for button. 您可以使用图像视图作为按钮。 Also you can use this method for changing image 您也可以使用此方法更改图像

override func tableView(_ tableView: UITableView, didSelec tRowAt indexPath: IndexPath) {}

Edit: 编辑:

I made a table view cell class: 我做了一个表格视图单元格类:

class TableViewCell: UITableViewCell {

    @IBAction func bAction(_ sender: Any) {
         button.titleLabel?.text = "asdaf"
    }

    @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
}

}

then I edit tableView function: 然后我编辑tableView函数:

 override func tableView(_ tableView: UITableView, cellForRowAt    indexPath: IndexPath) -> UITableViewCell {


       let cell: TableViewCell! = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) as? TableViewCell
       cell.textLabel?.text = "abc"
    // Configure the cell...

       cell.bAction(Any.self)
       return cell
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM