2

カスタム セルのボタンをタップしてから下 (または上) にスクロールすると、別のセル ボタンもタップされます。ボタン用に作成したボタンアウトレットが無効になっているため、タップされていることがわかります。

MycellForRowAtIndexPathには、セルの reuseIdentifier があります。

var cell: FeedTableViewCell? = tableView.dequeueReusableCellWithIdentifier("MusicCell") as? FeedTableViewCell

私が持っていることを考えると、degueueReusableCellWithIdcellForRowAtIndexPathは必要prepareForReuseですか?カスタム セル ファイルに を追加するprepareForReuseと、セルはデフォルト値に戻ります (明らかに、デフォルト値にリセットしたためです)。問題は、各 indexPath.row の値を保持することです。

これは私が値を照会する方法です:

 override func queryForTable() -> PFQuery {
        var query:PFQuery = PFQuery(className:"Music")

        if(objects?.count == 0)
        {
            query.cachePolicy = PFCachePolicy.CacheThenNetwork
        }

        query.orderByAscending("videoId")

        return query
    }

これはnumberOfRowsInSectioncellForRowAtIndexPath

 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
     return  objects!.count
    }


   override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {

        var cell: FeedTableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? FeedTableViewCell
        if(cell == nil) {
            cell = NSBundle.mainBundle().loadNibNamed("FeedTableViewCell", owner: self, options: nil)[0] as? FeedTableViewCell
        }
        if let pfObject = object {
               //I took out the irrelevant methods. I can add them if that makes a difference... 
                var votes:Int? = pfObject["votes"] as? Int
                if votes == nil {
                    votes = 0
        }
        cell?.votesLabel?.text = "\(votes!)"

}

viewDidLoadこれを上記の super.viewDidLoad()に登録しています

 tableView.registerNib(UINib(nibName: "FeedTableViewCell", bundle: nil), forCellReuseIdentifier: cellIdentifier)

これは、customCell のボタン クエリです。

@IBAction func heartButton(sender: AnyObject) {
        if(parseObject != nil) {
            if var votes:Int? = parseObject!.objectForKey("votes") as? Int {
                votes!++

                parseObject!.setObject(votes!, forKey: "votes")
                parseObject!.saveInBackground()

                votesLabel?.text = "\(votes!)"
            }
        }
        heartOutlet.enabled = false
}

どんな助けや提案も大きな意味があります。

ありがとうございました。


私が使用した参照リンク:

いくつかのリンクを参照しましたが、それらはobjective-cにあり、役に立ちませんでした:

UICollectionView タップで複数のセルを選択

prepareForReuse メソッドの使用方法

ドキュメントも参照しましたが、あまり役に立ちませんでした。

4

1 に答える 1

2

enabled投稿したコードから、のプロパティをUIButton設定していないことは明らかですDataSource(テーブルビューをロードするために使用している配列とそのオブジェクト、つまりobjects配列の要素)。配列に含まれるオブジェクトが何であれ、ボタンの条件が真か偽かを決定するプロパティを追加し、それにcellForRowAtIndexPath応じてボタンの有効なプロパティを設定します。ボタンがクリックされたら、(デリゲートを使用して) ViewController にコールバックを追加し、そこにプロパティを設定します。

サンプルコード

カスタムセルクラス:

protocol CellButtonDelegate
{
    func buttonClicked(cell : PFTableViewCell)
}

public var delegate : CellButtonDelegate?

public var buttonEnabled : Bool?
{
    get
    {
        return heartOutlet.enabled
    }
    set
    {
        heartOutlet.enabled = newValue
    }
}

@IBAction func heartButton(sender: AnyObject) {
        if(parseObject != nil) {
            if var votes:Int? = parseObject!.objectForKey("votes") as? Int {
                votes!++

                parseObject!.setObject(votes!, forKey: "votes")
                parseObject!.saveInBackground()

                votesLabel?.text = "\(votes!)"
            }
        }
        delegate?.buttonClicked(self)
}

ViewController で:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {

        var cell: FeedTableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? FeedTableViewCell
        if(cell == nil) {
            cell = NSBundle.mainBundle().loadNibNamed("FeedTableViewCell", owner: self, options: nil)[0] as? FeedTableViewCell
        }
        if let pfObject = object {
               //I took out the irrelevant methods. I can add them if that makes a difference... 
                var votes:Int? = pfObject["votes"] as? Int
                if votes == nil {
                    votes = 0
        }
        cell?.buttonEnabled = objects[indexPath.row].isEnabled //The new property you need to add. true by default
        cell?.delegate = self //Make sure you implement the delgate
        cell?.votesLabel?.text = "\(votes!)"    
        return cell?
}

func buttonClicked(cell : PFTableViewCell)
{
     //Here, get the indexPath using the cell and assign the new property in the array.
}

上記のコードは大雑把であることに注意してください。コードからアイデアを得て、要件に従って実装するだけです。

于 2015-11-26T13:03:09.697 に答える