【问题标题】:Values Repeat When Scrolling in TableView在 TableView 中滚动时值重复
【发布时间】:2016-02-27 04:44:02
【问题描述】:

当我点击自定义单元格中的按钮然后向下(或向上)滚动时,也会点击另一个单元格按钮。我看到它被点击了,因为我为按钮创建的按钮插座被禁用了。

我的cellForRowAtIndexPath 有一个用于单元格的重用标识符:

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

考虑到我在cellForRowAtIndexPath 中有degueueReusableCellWithId,我需要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!)"

}

我在 super.viewDidLoad() 上方的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 Tap Selects More Than One Cell

How to use prepareForReuse method

我还参考了文档,但这并没有太大帮助。

【问题讨论】:

  • 添加UITableView数据源方法的代码
  • 我在哪里从 tableView 查询和发送数据?还是来自自定义单元格? @HarikrishnanT
  • tbale查看数据源方法包括cellForRowAtIndexPathnumberOfCellsInTableView等,请贴出cellForRowAtIndexPath的代码(如果你也在使用willDisplayCell)。一定是你做错了什么导致了问题。
  • 刚刚更新了问题,谢谢@HarikrishnanT

标签: swift uitableview custom-cell prepareforreuse


【解决方案1】:

从您发布的代码中,很明显您没有设置UIButtonenabled 属性相对于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.
}

请注意,上面的代码是粗略的。只需从代码中获取想法并根据您的要求实施即可。

【讨论】:

  • 我使用 ParseUI 来获取对象,所以 objects 数组是存储这些对象的 ParseUI 属性。你能提供一些示例代码吗?我无法理解应该放置的逻辑。它不只与cellForRowAtIndexPath 相关吗?如果我的 UIButton 和数据源有问题,为什么它会与 objects 数组相关? @HarikrishnanT
  • 添加了示例代码。参考我发布的代码后,您将不得不对当前逻辑进行一些更改。
  • 现在就试试吧!非常感谢!
  • 2 个问题:我收到此错误:Cannot assign value of type "FeedTableViewController" to type CellButtonDelegate? 对于此链接:'cell?.delegate = self` 我在单元格中添加了委托,如下所示:var delegate : CellButtonDelegate? 就在类定义的下方细胞。对于这个函数:func buttonClicked(cell : PFTableViewCell){ cell.buttonEnabled = objects![indexPath.row].isEnabled = true },我把它放在其他方法下面的cellForRowAtIndexPath,我得到这个错误:value of type PFTableViewCell has no member buttonEnabled。建议?
  • 在将委托添加到类定义时,我也遇到了错误*。 @HarikrishnanT
猜你喜欢
  • 2016-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多