2017-03-26 9 views
0

このビューコントローラを完全に書き換えた後も、テーブルビューの最後の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) 
     } 
    } 
+2

これ以上の情報を伝えるのは不可能です。 'cellForRow(at:)'メソッドを投稿し、IBでセルプロトタイプを設定した場合は、テーブルビューのセルの接続インスペクタのスクリーンショットも表示します。 (ボタンにリンクされた複数のIBActionsを持つことは可能ですが、何が起こっているかについての良い推測のように思えます) –

+0

@DuncanC質問を編集しましたが、これでは不十分か、 on something – XvKnightvX

+1

'self'をボタンに複数回ターゲットとして追加します:' removePositionButton.addTarget(self、action:#selector(removePosition(_ :)):for .touchUpInside) 'ボタンにターゲットを追加すると、以前に追加されたターゲットは削除されません。以前のターゲットを削除する必要があります。 –

答えて

2

テーブルビューは、細胞を再利用します。基本的には、セルが画面を離れると、画面に入るように再利用されます(同じ再利用識別子がある場合)。 table​View(_:​cell​For​Row​At:​)は、テーブルビューにセルが必要なときはいつでも複数回呼び出されます。同じセルに対して複数回呼び出すこともできます(視野を離れてから再入力する場合など)。

この行またはそのような行は、セルがテーブルビューのどこかで使用されたときに追加した以前のターゲットと同じボタンに複数のターゲットを追加しています。あなたがする必要があるかもしれない何

removePositionButton.addTarget(self, action: #selector(removePosition(_:)), for: .touchUpInside) 

は、新しいものを追加する前に、ターゲットを削除するか、table​View(_:​cell​For​Row​At:​)からaddTarget呼び出しを削除です。

ターゲットを削除する方法については、UIControlのドキュメントをご覧ください。

*ヒント:remove​Target(_:​action:​for:​)

+0

removePositionButton.allTargetsをチェックすると、正しい現在のターゲット数がわかりますか? (私はallTargetsをチェックして、それは私に1だけを示す) – XvKnightvX

0

が、それはあなたのボタンが(誤って)であることが可能です複数のアクションにリンクされていますか?

1

あなたは

button.addTarget(self, action: #selector(action1(_:)))

、その後

button.addTarget(self, action: #selector(action2(_:)))

を使用する場合は、あなたのボタンに関連付けられた2つのアクションが、唯一の1つの目標を持っています。同じターゲットに対して2つのアクションを追加しました。したがって、allTargetsプロパティは、ボタンに2つのターゲット/アクションが接続されていても、1つのターゲットを表示します。

同じターゲットと同じコントロールイベントで複数のアクションを追加した場合は、それらを見つけるのにactions(forTarget:forControlEvent:)を使用する必要があります。(あなたの場合、actions(forTarget: self, forControlEvent:, .touchUpInside)

cellForRow(at:)の方法では、ボタンのターゲット/アクションの数をチェックして、アクションカウントがゼロの場合は新しいターゲット/アクションをボタンに追加するか、すべてを削除する必要があります新しいターゲットを追加する前にターゲット/アクションを実行します。

ここでの経験則は、セルがリサイクルされるということです。返されるセルは、最後に使用された時の設定が残っていると仮定し、常にセル内のすべてのビューを構成する必要があります。

関連する問題