このビューコントローラを完全に書き換えた後も、テーブルビューの最後の2つのセクションに2つのボタンが表示されています。私は2つのボタンを持っています、セクション3のそれらのうちの1つはセクション4の他のものです(テーブルビューは5つしかないので、これらは最後の2です)。問題は、これらのボタンの1つをクリックすると、別のものを選択するまで(同じテーブルビュー内にファクト以外のものが接続されていない)、別のボタンをクリックするとそのセクションに1が追加されます他のセクションに1を加えます。私はターゲットの数をチェックしてみたのかもしれませんが、両方のボタンの機能を1つのボタンに追加するのかどうかを見てみましたが、1つのターゲットしか持っていなくても両方の機能が呼び出されています... Iveは、どのような助けをされても大変感謝しています(コードを投稿しないと申し訳ありませんが、かなり複雑なファイルがありますので、私があなたに見たいものを教えてください)。TableViewCellのUIButtonは2つの関数を呼び出す
EDIT - これは私がUITableViews cellForRowAtのセクション4のために与えるものです(cellForRowAtでセクション3とセクション4は、いくつかの命名の違い以外同じです)
} else {
if indexPath.row == 0 {
cell = tableView.dequeueReusableCell(withIdentifier: "Section Card", for: indexPath)
let positionsSectionCard = cell.contentView.viewWithTag(1) as! UILabel
positionsSectionCard.text = "Positions"
} else if indexPath.row == 1 {
cell = tableView.dequeueReusableCell(withIdentifier: "Picker Cell", for: indexPath)
let positionPicker = cell.contentView.subviews[0].subviews[1].subviews[0] as! UIPickerView
let addPositionButton = cell.contentView.viewWithTag(4) as! UIButton
let positionList = Array(positions.keys).sorted()
var positionCounter = 0
var enabledPositionsCounter = 0
positionPicker.dataSource = self
positionPicker.delegate = self
while positionCounter < positionList.count {
if positions[positionList[positionCounter]]! {
enabledPositionsCounter = enabledPositionsCounter + 1
}
positionCounter = positionCounter + 1
}
if enabledPositionsCounter == positions.count {
addPositionButton.isEnabled = false
addPositionButton.setTitleColor(UIColor.gray, for: .normal)
} else {
addPositionButton.isEnabled = true
addPositionButton.setTitleColor(UIColor.white, for: .normal)
}
addPositionButton.titleLabel?.numberOfLines = 2
addPositionButton.titleLabel?.textAlignment = NSTextAlignment.center
addPositionButton.setTitle("Add\nPosition", for: .normal)
addPositionButton.addTarget(self, action: #selector(addPosition), for: .touchUpInside)
} else {
cell = tableView.dequeueReusableCell(withIdentifier: "Item Card", for: indexPath)
let positionLabel = cell.contentView.viewWithTag(1) as! UILabel
let removePositionButton = cell.contentView.viewWithTag(2) as! UIButton
let positionList = Array(positions.keys).sorted()
var positionCounter = 0
var usedPositionsList: [String] = []
positionLabel.adjustsFontSizeToFitWidth = true
positionLabel.numberOfLines = 2
if eventSettingsTableView.numberOfRows(inSection: 4) < 4 {
while positionCounter < positionList.count {
if positions[positionList[positionCounter]]! {
positionLabel.text = positionList[positionCounter]
}
positionCounter = positionCounter + 1
}
} else {
while positionCounter < eventSettingsTableView.numberOfRows(inSection: 4) {
if eventSettingsTableView.cellForRow(at: IndexPath(row: positionCounter + 2, section: 4))?.contentView.subviews[0].subviews[1].subviews[0] != nil {
print((eventSettingsTableView.cellForRow(at: IndexPath(row: positionCounter + 2, section: 4))?.contentView.subviews[0].subviews[1].subviews[0] as! UILabel).text!)
usedPositionsList.append((eventSettingsTableView.cellForRow(at: IndexPath(row: positionCounter + 2, section: 4))?.contentView.subviews[0].subviews[1].subviews[0] as! UILabel).text!)
}
positionCounter = positionCounter + 1
}
positionCounter = 0
while positionCounter < positionList.count {
if !usedPositionsList.contains(positionList[positionCounter]) && positions[positionList[positionCounter]]! {
positionLabel.text = positionList[positionCounter]
}
positionCounter = positionCounter + 1
}
removePositionButton.addTarget(self, action: #selector(removePosition(_:)), for: .touchUpInside)
}
}
これ以上の情報を伝えるのは不可能です。 'cellForRow(at:)'メソッドを投稿し、IBでセルプロトタイプを設定した場合は、テーブルビューのセルの接続インスペクタのスクリーンショットも表示します。 (ボタンにリンクされた複数のIBActionsを持つことは可能ですが、何が起こっているかについての良い推測のように思えます) –
@DuncanC質問を編集しましたが、これでは不十分か、 on something – XvKnightvX
'self'をボタンに複数回ターゲットとして追加します:' removePositionButton.addTarget(self、action:#selector(removePosition(_ :)):for .touchUpInside) 'ボタンにターゲットを追加すると、以前に追加されたターゲットは削除されません。以前のターゲットを削除する必要があります。 –