2016-10-01 18 views
1

私は、ユーザーが異なるトピックにエントリーを書き込んだ後、エントリーに上下のポイントを与えることができるアプリを開発しています。私は私のtableViewControllerで機能使用:Swiftの異なるTableViewセルのボタンに異なるタグを与える方法

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

を、私は、この関数の最後に次の2行を追加しました:

cell.plusButton.tag = indexPath.row 
cell.minusButton.tag = indexPath.row 

だから、これはタグをのtableView内のすべてのボタンを与えるべきであるように、そのそのセルのindexpath.rowと同じですが、間違っていますか?ボタンを呼び出そうとすると、そのタグはすべて同じで、0になります。どうすれば別のタグを付けることができますか?このようにする方法はありませんか?

これは私がボタンを呼びたい時にコードがあるものである:

@IBAction func plus(sender: AnyObject) { 

    print(self.tag) 

    let ref = FIRDatabase.database().reference().child("topics/"+topicClicked+"/"+entriesArrayTwo[self.tag]) 

    var value = Int() 
    var date = String() 
    var user = String() 
    var votedDown = [""] 
    var votedUp = [""] 

    ref.observeSingleEventOfType(.Value, withBlock: { snapshot in 
     let dict = snapshot.value as! [String: AnyObject] 

     value = dict["point"] as! Int 
     date = String(dict["date"]!) 
     user = String(dict["user"]!) 
     votedUp = dict["votedUp"] as! NSArray as! [String] 
     votedDown = dict["votedDown"] as! NSArray as! [String] 

     var tempBool = false 
     var temp = -1 

     for uid in votedDown { 
      temp = temp + 1 

      if uid == FIRAuth.auth()?.currentUser?.uid { 
       votedDown.removeAtIndex(temp) 
       tempBool = true 
      } 
     } 

     if tempBool == false { 
      votedUp.append((FIRAuth.auth()?.currentUser?.uid)!) 
     } 

     ref.setValue(["point": value+1, "date": date, "user": user, "votedDown": votedDown, "votedUp": votedUp]) 

     self.point.text = String(value+1) 
    }) 

    if minusButton.hidden == true { 
     minusButton.hidden = false 
    } else { 
     plusButton.hidden = true 
    } 
} 

私のtableView(のtableView:のUITableView、cellForRowAtIndexPath indexPath:NSIndexPath) - > UITableViewCellの機能は以下の通りです:

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

    if self.resultSearchController.isActive { 

     let ref = FIRDatabase.database().reference().child("topics/"+topicClicked+"/"+filteredTableData[(indexPath as NSIndexPath).row]) 

     ref.observeSingleEvent(of: .value, with: { snapshot in 

      let value = snapshot.value as? NSDictionary 

      cell.point.text = String(describing: value!["point"]!) 

      let postRef = FIRDatabase.database().reference().child("users/"+String(describing: value!["user"]!)) 

      postRef.observeSingleEvent(of: .value, with: { snapshotTwo in 

       let valueTwo = snapshotTwo.value as? NSDictionary 

       cell.subInfo.text = String(describing: valueTwo!["name"]!)+" "+String(describing: valueTwo!["surname"]!)+" - "+String(describing: value!["date"]!) 

      }) 


      }) 

     cell.entry.text = self.filteredTableData[(indexPath as NSIndexPath).row] 

    } else { 

     let ref = FIRDatabase.database().reference().child("topics/"+topicClicked+"/"+entriesArray[(indexPath as NSIndexPath).row]) 

     ref.observeSingleEvent(of: .value, with: { snapshot in 

      let value = snapshot.value as? NSDictionary 

      cell.point.text = String(describing: value!["point"]!) 

      let postRef = FIRDatabase.database().reference().child("users/"+String(describing: value!["user"]!)) 

      postRef.observeSingleEvent(of: .value, with: { snapshotTwo in 

       let valueTwo = snapshotTwo.value as? NSDictionary 

      cell.subInfo.text = String(describing: valueTwo!["name"]!)+" "+String(describing: valueTwo!["surname"]!)+" - "+String(describing: value!["date"]!) 

      }) 

      let votedUpRef = ref.child("votedUp") 

      votedUpRef.observeSingleEvent(of: .value, with: { upSnapshot in 

       var tempDict = snapshot.value as! [String: AnyObject] 
       let tempArray = tempDict["votedUp"] as! [String] 

       for uid in tempArray { 

        if String(uid) == FIRAuth.auth()?.currentUser?.uid { 

         cell.plusButton.isHidden = true 

        } 

       } 

      }) 

      let votedDownRef = ref.child("votedDown") 

      votedUpRef.observeSingleEvent(of: .value, with: { upSnapshot in 

       var tempDict = snapshot.value as! [String: AnyObject] 
       let tempArray = tempDict["votedDown"] as! [String] 

       for uid in tempArray { 

        if String(uid) == FIRAuth.auth()?.currentUser?.uid { 

         cell.minusButton.isHidden = true 

        } 

       } 

      }) 

     }) 

     cell.entry.text = self.entriesArray[(indexPath as NSIndexPath).row] 

    } 

    cell.plusButton.tag = (indexPath as NSIndexPath).row 
    cell.minusButton.tag = (indexPath as NSIndexPath).row 

    // NEW METHOD TO GET THE BUTTON 

    let check1: UIButton = (cell.viewWithTag(1) as! UIButton) 
    let check2: UIButton = (cell.viewWithTag(2) as! UIButton) 
    check1.addTarget(self, action: #selector(HubEntriesTableViewController.CloseMethod(_:event:)), for: .touchDown) 
    check2.addTarget(self, action: #selector(HubEntriesTableViewController.CloseMethod1(_:event:)), for: .touchDown) 

    // Configure the cell... 

    return cell 
} 
+0

ボタンをクリックするとあなたのコード全体を貼り付けることができます –

+0

私は、データベースを参照しています。子供が押されたボタンのタグから来ています。私のケースでは、タグは常に0です。 –

+0

set cell.buttonPlus.Tag = indexpath.row + UniqueNumber は、次のようになります。 ref = FIRDatabase.database()。referance()。子( "self.tag") cell.buttonMinus.Tag = indexpath.row + UniqueNumber(different) そしてあなたはそれを簡単に得ることができます。 –

答えて

1

おそらくようやく問題が見つかりました。私のプロジェクトで問題を再現すると、にダウンキャストしてUIButtonが欠落していることがわかりました。

のでHubEntryTableViewCellサブクラス内、このような方法を更新します。

@IBAction func plus(sender: AnyObject) { 

    // self.tag, if called from UITableViewCell subclass, is rather cell's tag, not button's tag 

    let button = sender as! UIButton 
    print("button.tag = \(button.tag)") 
    ... 
} 
+0

私はセクションが1つしかありません –

+0

indexpath.rowsが0,1,2というように正しく表示されますに。それには問題ありません。また、私が 'print(cell.plusButton.tag)'を実行すると、正しく0,1,2が得られます。@pedrouan –

0

あなたは、あなたがタグを心配しないでください、このコードを試してみてくださいテーブルビューでのボタンのクリックに対してアクションを実行している場合...

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("Eventcell", forIndexPath: indexPath) 
    let check1: UIButton = (cell.viewWithTag(11) as! UIButton) 
    let check2: UIButton = (cell.viewWithTag(12) as! UIButton) 
    check1.addTarget(self, action: #selector(EventListController.CloseMethod(_:event:)), forControlEvents: .TouchDown) 
    check2.addTarget(self, action: #selector(EventListController.CloseMethod1(_:event:)), forControlEvents: .TouchDown) 
    return cell 
} 
    @IBAction func CloseMethod(sender: UIButton, event: AnyObject) { 
     let touches = event.allTouches()! 
     let touch = touches.first! 
     let currentTouchPosition = touch.locationInView(self.Eventlisttable) 
     let indexPath = self.Eventlisttable.indexPathForRowAtPoint(currentTouchPosition)! 
     print("\(Int(indexPath.row))") 

    } 
    @IBAction func CloseMethod1(sender: UIButton, event: AnyObject) { 
     let touches = event.allTouches()! 
     let touch = touches.first! 
     let currentTouchPosition = touch.locationInView(self.Eventlisttable) 
     let indexPath = self.Eventlisttable.indexPathForRowAtPoint(currentTouchPosition)! 
     print("\(Int(indexPath.row))") 
} 

私の答えがあなたの投票に役立つならば。ありがとうございます..

+0

私がしたいことは、データベースからのエントリの "ポイント"値を増減することです。をクリックします。私は+ボタンが押されたときに1を増やしたいと思っています。 –

+0

この操作は、その特定のセルのCloseMethodで行うことができます。私の場合、私は特定のセルデータのtableviewでいくつかのアクションを実行する必要があります...私のテーブルはEventlisttableです。 –

+0

closeMethodとcloseMethod1とは何ですか?それらは異なるボタン用ですか?またcell.viewWithTag(11)と(12)、それらは何ですか? –

関連する問題