2

When I tap a button in a custom cell and then scroll down (or up) another cell button is also tapped. I see that it's tapped because the button outlet that I created for the button is disabled.

My cellForRowAtIndexPath has a reuseIdentifier for the cell:

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

Considering I have the degueueReusableCellWithId in the cellForRowAtIndexPath do I need a prepareForReuse? When I add the prepareForReuse in my custom cell file, the cell just goes back to the default values (obviously because I reset it to the default values). Problem is I want it to keep the value of each indexPath.row.

This is how I'm querying the values:

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

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

        query.orderByAscending("videoId")

        return query
    }

This is the numberOfRowsInSection and cellForRowAtIndexPath

 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!)"

}

I'm registering this in the viewDidLoad above the super.viewDidLoad()

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

This is my button query in the 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
}

Any help and suggestions mean a lot.

Thank you.


REFRENCE LINKS I USED:

I referred to several links but they were in objective-c and didn't help:

UICollectionView Tap Selects More Than One Cell

How to use prepareForReuse method

I also referred to the docs, and that didn't help much.

4

1 回答 1

2

从您发布的代码中,很明显您没有设置enabledUIButton对于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)
}

在视图控制器中:

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 回答