重复使用单元格中的 UIbutton currentTitle Swift

UIbutton currentTitle in cell reused Swift

目前我有一个带有自定义单元格的 uitableview。 它包含一个 uilabeluibutton - 从两个单独的数组中获取的数据源。 uibutton 函数是将 uilabel 的行附加到相应的数组并将它们插入 uitableview 以及在下面添加一个带有另一个 uibutton 的新单元格。

有一个条件 - 一旦问题数组值为 nil,就会显示答案 uibutton

问题是 - 因为 uibutton 名称是从数组中获取的最后一个值,一旦我滚动所有按钮标题 re-written 以匹配最新的。

这是流程。

这是我的代码

 func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return numberofsections
// is always equal to zero
} 


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return someTagsArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell     {

    var cell:TblCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! TblCell

        cell.lblCarName.text = someTagsArray[indexPath.row]
        cell.act1.setTitle(answersdict.last, forState:UIControlState.Normal)


return cell
}
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

var cell:TblCell = cell as! TblCell

    if let text = cell.lblCarName.text where text.isEmpty {
        cell.act1.hidden = false
    } else {
        println("Failed")
    }
}

有什么方法可以存储这些 uibutton 值或防止单元格被完全重复使用。

关于我的 question\answer 模型的更多数据 我有一个名为 linesmain 的嵌套数组的字典用于问题

linesmain = ["first":["question 1", "question 2", "question 3"], "answer 1": ["question 1", "question 2", "question 3"], "answer 2": ["question 1", "question 2", "question 3"], "answer 3":["question 1", "question 2", "question 3"]]

和一个类似的答案词典

answersmain = ["first": ["answer 1"], "answer 1": ["answer 2"], "answer 2": ["answer 3"], "answer 3":["answer 4"]]

我保持这种状态,因为我以后可能会增加答案的数量。

然后我有两个数组供单元格追加。 一个追加 - 问题 (someTagsArray),另一个追加 - 答案 (answersdict)。 在启动时加载 "first" 问题和答案,然后根据新添加的单元格中的 uibutton currentTitle。

谢谢。

不,没有办法阻止单元格被重复使用。重用可保持较低的内存要求和快速滚动,因为 table 仅保留足够的单元格以确保它可以显示可见的行。

当您来回滚动时,单元格将不断被重复使用。这是正常的、预期的,并不意味着可以规避。

您必须将这些答案值存储在您的模型中,以便您可以通过 indexPath 行查找答案。

更改 answersdict.last 以使用某种获取一行的方法,以及 returns 该行的答案文本。

如果您不确定该怎么做,您将需要 post 更多代码来展示您的模型如何存储您的问答数据。

更新

总的来说,我会做一些不同的事情,您可能需要考虑一下。

  1. 一个table查看单元格已选择table。您可以让用户选择(点击)单元格来完成按钮事件的工作,而不是向单元格添加按钮。

  2. 一样,您最好将数据分成几个部分,每个部分包含该部分的问题和答案:

    Section 0
        Question 1 // row 0 of section 0
        Question 2 // row 1 of section 0
        Question 3
        Answer 1
    Section 1
        Question 1 // row 0 of section 1
        Question 2 // row 1 of section 1
        and so forth...

您的 someTagsArray 最好存储为 [[问题 1,问题 2,问题 3],[问题 1,问题 2,问题 3],...]。您看到每组问题如何在其自己的(部分)数组中了吗?

然后您可以通过查询 someTagsArray[indexPath.section][indexPath.row] 获取每个问题的文本,通过查询 answersArray[indexPath.section].

获取每个答案的文本
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return someTagsArray.count
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return someTagsArray[section].count + 1 // number of Questions + 1 for the answer
}

更新二:

您看到数据如何细分的示例了吗?让我们看一下第一部分的计数,即第 0 部分:

someTagsArray[0].count // 3 questions in section 0
answersArray[0] // the one answer for section 0

问题和答案的数量不相等。正如您所指出的,那是没有意义的。

现在我们看到第 0 部分有 3 个问题和 1 个答案,我们知道第 0 部分有 4 行。

前三行是问题。最后一行是答案。

因此,在 tableView:cellForRowAtIndexPath 中,您将不得不检查 indexPath.row,并确定它是问题行还是答案行。这应该很容易做到,我会把它留作练习。

如果 table 中的行数超过了屏幕上可以显示的行数,那么您的方法就有问题。在那种情况下,滚动 off-screen 的单元格将是 re-used,按钮的内容将丢失。如果您可以确定这永远不会发生,请使用单独的 datasource 数组作为按钮标题。

您必须创建一个包含按钮标题的单独数组,您可以将它们用于 indexPath.row。希望这有帮助