滚动时,不准确的数据返回到快捷按钮

时间:2016-04-03 00:51:33

标签: swift uitableview

我希望通过显示" cellForRowAtIndexPath"来提供足够的信息。分割。整个计划太长了。 tableView中的每一行代表一个玩家,每个玩家都有四个与他/她相关的按钮。基于播放器的动作,我将按下其中一个按钮来记录该动作,并在该按钮的titleLabel中按下每个按钮。我为每个按钮创建了一个数组,以跟踪每个按钮中按下按钮的次数," plyZeroes"," plrOnes"," plrTwoes",和" plrThrees"。我使用这些数组(plrZeroesArray,plyrOnesArray等)来保存正确的值,然后在它被重新显示之前重新加载按钮titleLabel并使用这个正确的值(如果它已经滚动离开屏幕然后重新出现)。至少我认为我在做什么。但可能是我对cellForRowAtIndexPath的工作方式缺乏了解。

在下面的代码段中,我设置了断点并放置了" print"在各个地方的陈述,我相信该阵列已经为每一个" plrxxx"分配了正确的信息。在"返回单元格之前的按钮"声明。但是,当我运行应用程序并且如果我足够滚动设备(iPad)时,设备上显示的计数(标签文本)与数组中的值不同(不同于分配给按钮的值&# 39; s textLabel)。如果我再次滚动以显示单元格,则值将变为正确。基本上当一些细胞消失并重新出现时,包含不正确的值约为30% - 50%的时间。有什么想法吗?

ps:还有另一个对象" plrAvg"这是一个标签,我会在返回之前保存相同的数据,并且它始终正常工作。

var plrNumbers: NSMutableArray! = NSMutableArray()
var players: NSMutableArray! = NSMutableArray()

var plrAverageArray = [String](count: 99, repeatedValue: "0")

var plrZeroesArray = [String](count: 99, repeatedValue: "0")
var plrOnesArray = [String](count: 99, repeatedValue: "0")
var plrTwosArray = [String](count: 99, repeatedValue: "0")
var plrThreesArray = [String](count: 99, repeatedValue: "0")

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

    let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell


    // Poputate the players' info, average, and last action//
    cell.hiddenLabel.text = ("\(indexPath.row)")

    cell.plrNumbers.text = (plrNumbers[indexPath.row] as! String)
    cell.plrNames.text = (players[indexPath.row] as! String)



    cell.plrZeroes.addTarget(self, action: "handleButtonPressed:", forControlEvents: .TouchUpInside)
    cell.plrOnes.addTarget(self, action: "handleButtonPressed:", forControlEvents: .TouchUpInside)
    cell.plrTwos.addTarget(self, action: "handleButtonPressed:", forControlEvents: .TouchUpInside)
    cell.plrThrees.addTarget(self, action: "handleButtonPressed:", forControlEvents: .TouchUpInside)
    cell.btnUndoLastPlrAction.addTarget(self, action: "handleButtonPressed:", forControlEvents: .TouchUpInside)


    //restore the player's average in case it was lost because it scrolled off the screen
    cell.plrAvg.text = (plrAverageArray[indexPath.row])

    //restore the button text in case it was lost because it scrolled off the screen
    cell.plrZeroes.titleLabel?.text = (plrZeroesArray[indexPath.row])
    cell.plrOnes.titleLabel?.text = (plrOnesArray[indexPath.row])
    cell.plrTwos.titleLabel?.text = (plrTwosArray[indexPath.row])
    cell.plrThrees.titleLabel?.text = (plrThreesArray[indexPath.row])

print("Buttons \(indexPath.row)] = ", plrZeroesArray[indexPath.row] + plrOnesArray[indexPath.row] + plrTwosArray[indexPath.row] + plrThreesArray[indexPath.row])

        return cell
    }

0 个答案:

没有答案