私はコレクションビューとボタンの内側にあります。 このボタンをタップすると、その名前を変更したいと思います。 私はそれをすることができましたが、私はそれをタップすると、私は20のボタンが10個あればこのように変化します。最初のボタンをタップすると0,2,4,6,8が変わり、リスト内では、すべてのボタンがチェックされます。私はあまりにもif文なしで試してみましたセル内のボタンのタイトルを変更する方法Swift
@IBAction func following(sender: AnyObject) {
if follow.tag == sender.tag {
follow.setTitle("Following", forState: .Normal)
}
print("\(follow.tag) \(sender.tag)")
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Interest Cell", forIndexPath: indexPath) as! ThirdTabCell
cell.follow.tag = indexPath.row
return cell
}
ボタンの機能の内部ではなく、再び同じ問題:
は、これは私のコードです。 タグを印刷すると、タップされたボタンだけが印刷されます。
は、私も自分のViewControllerに
cell.follow.addTarget(self, action: #selector(ThirdTab.follow(_:)), forControlEvents: UIControlEvents.TouchUpInside)
//inside cellForItemAtIndexPath
func follow(sender:UIButton!) {
}
私はどこに次のように尋ねてもいいですか?再使用可能なセルからIBActionを取得できないようにする必要があります。また、私は送信者がAnyObjectの代わりにUIButtonになるはずですか? – Happiehappie
ibaction関数がセルクラスの内側にある –
VCでセレクタメソッドを作成することをお勧めします。 – Happiehappie