[英]How to change button image on tapped in repeating cell?
我正在做待辦事項應用程序,我需要對按鈕進行操作,點擊該按鈕將更改我點擊的單元格中的圖像。 單元從Firebase檢索數據到UITableView。 我使用了按鈕和圖像插座,並且都報告了此錯誤:
從TaskViewController到UIButton的按鈕無效。 插座不能連接到重復內容。
我有這段代碼用於檢索數據到單元格:
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
}
我在情節提要中有這個
標簽左側的對象是button。
按鈕圖片設置在情節提要中。
我不知道該怎么做,因為我經驗不足。 請幫我。
嘗試:
Class
設置為TasksCell
(在我的示例中為WorkTableViewCell
)。 Module
字段中單擊,將其清空,然后按Enter。 它應該更改為Current - project_name_here
(例如, Current - Artistry
。 這是它的外觀截圖。 檢查它以確保您以正確的順序選擇了正確的項目:
然后,從連接檢查器中刪除單元的舊插座(按鈕,標簽,所有內容)。 在選擇單元格時,然后在選擇視圖控制器時執行該步驟(但在選擇視圖控制器時, 僅刪除“完成”按鈕的插座)。
最后,通過打開TasksCell
類的代碼(在新窗口中)而不是 TaskViewController
類(可能在TasksCell.swift
),並直接在下面的行中添加代碼 :
class TasksCell: UITableViewCell {
(或類似),按住控件並拖動按鈕,標簽等,如下所示:
最后,完成僅從標簽,按鈕 等到TasksCell類的新插座的創建( 如前所述) (按“連接”之前,請參閱下面的注釋):
根據需要更改Name
。 標簽可以是taskName
,按鈕可以是completedButton
。 請勿更改“ Type
或任何其他字段。 按“連接”,問題代碼便可以正常工作。
在TasksCell類中,將按鈕連接到單元格,您可以更輕松地訪問它(是的,同意@ingraham,您不能將其連接到視圖控制器):
class TasksCell: UITableViewCell {
@IBOutlet weak var buttonCheckbox: UIButton!
@IBOutlet weak var taskName: UILabel!
@IBOutlet weak var updatedDate: UILabel!
}
您可以使用圖像視圖作為按鈕。 您也可以使用此方法更改圖像
override func tableView(_ tableView: UITableView, didSelec tRowAt indexPath: IndexPath) {}
編輯:
我做了一個表格視圖單元格類:
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
}
}
然后我編輯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.