简体   繁体   English

单元格中的 UIbutton currentTitle 重用 Swift

[英]UIbutton currentTitle in cell reused Swift

Currently i have an uitableview with a custom cell.目前我有一个带有自定义单元格的uitableview It contains an uilabel and uibutton - data source for which is fetched from two separate arrays.它包含一个uilabeluibutton - 从两个单独的数组中获取的数据源。 The uibutton function is to append the corresponding array with lines for the uilabel and insert them in the uitableview as well as add a new cell with another uibutton below.uibutton功能是对应的阵列与用于线追加uilabel并在将它们插入uitableview以及与另一种添加新的小区uibutton下方。

There is one conditional - once the question array value is nil, the answer uibutton is shown.有一个条件 - 一旦问题数组值为 nil,就会显示答案uibutton

The issue is - because the uibutton name is fetched as the last value from the array, once i scroll all the button titles are being re-written to match the latest one.问题是 - 因为uibutton名称是从数组中获取的最后一个值,所以一旦我滚动,所有按钮标题都将被重写以匹配最新的标题。

Here is the flow.这是流程。 在此处输入图片说明

Here is my code这是我的代码

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

Is there any way to store those uibutton values or prevent the cell from being reused completely.有什么方法可以存储这些uibutton值或防止单元格被完全重用。

Some more data on my question\\answer model I have a dictionary with the nested array called linesmain for questions关于我的问题\\答案模型的更多数据我有一个带有嵌套数组的字典,称为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"]]

and a similar dictionary for answers和类似的答案字典

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

i keep it that way because i might increase the amount of answers in the future.我保持这种方式,因为我将来可能会增加答案的数量。

Then i have two arrays for the cells to append.然后我有两个数组用于要附加的单元格。 One appends - the questions (someTagsArray), another one - the answers (answersdict).一个附加 - 问题(someTagsArray),另一个 - 答案(answersdict)。 On launch the "first" questions and answers are loaded, then depending on the uibutton currentTitle in the newly appended cell.在启动时加载“第一个”问题和答案,然后取决于新附加单元格中的uibutton currentTitle。

Thank you.谢谢你。

No, there's no way to prevent a cell from being reused.不,没有办法防止单元格被重用。 Reuse keeps memory requirements low, and scrolling fast, as the table only keeps around enough cells to ensure it can show the rows that are visible.重用可以降低内存需求并快速滚动,因为表格只保留足够多的单元格以确保它可以显示可见的行。

Cells will constantly be reused as you scroll back and forth.当您来回滚动时,单元格将不断重复使用。 This is normal, expected, and isn't meant to be circumvented.这是正常的、预期的,并不意味着要规避。

You're going to have to store those answer values in your model in a way that you can lookup the answer by indexPath row.您将不得不以一种可以通过 indexPath 行查找答案的方式将这些答案值存储在您的模型中。

Change answersdict.last to use some method that takes a row, and returns the answer text for that row.answersdict.last更改为使用某种方法来获取一行,并返回该行的答案文本。

If you're not sure how to do that, you'll need to post more code showing how your model stores your question and answer data.如果您不确定如何执行此操作,则需要发布更多代码来显示您的模型如何存储您的问题和答案数据。

Update更新

In general, there are a few things I'd do differently, that you might want to consider.一般来说,我会以不同的方式做一些事情,您可能需要考虑一下。

  1. A tableView cell is selectable. tableView 单元格是可选择的。 Instead of adding buttons to the cell, you could just let the user selecting (tapping) a cell accomplish the work of the button event.您可以让用户选择(点击)一个单元格来完成按钮事件的工作,而不是向单元格添加按钮。

  2. As the answer to your original question suggested , you'd be better off dividing the data into sections, with each section containing the questions and answer for that section:正如您对原始问题的回答所建议的那样,您最好将数据分成几个部分,每个部分都包含该部分的问题和答案:

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...

Your someTagsArray would be better off stored like [[question 1, question 2, question 3],[question 1, question 2, question 3], ...].您的someTagsArray好像 [[问题 1,问题 2,问题 3],[问题 1,问题 2,问题 3],...] 那样存储。 You see how each set of questions is in its own (section) array?您看到每组问题如何在自己的(部分)数组中?

Then you can get the text for each question by querying someTagsArray[indexPath.section][indexPath.row] and the text for each answer by querying answersArray[indexPath.section] .然后你就可以通过查询得到每个问题的文本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
}

Update 2:更新 2:

Do you see the example of how the data is broken down into sections?你看到数据如何分解成部分的例子了吗? Let's look at the counts for the first section, which is section 0:让我们看看第一部分的计数,即第 0 部分:

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

The count of questions and answers aren't equal.问题和答案的数量不相等。 As you pointed out, that would make no sense.正如你所指出的,这没有任何意义。

So, now that we see there are 3 questions and 1 answer for section 0, we know that section 0 has 4 rows.所以,现在我们看到第 0 部分有 3 个问题和 1 个答案,我们知道第 0 部分有 4 行。

The first three rows are for the questions.前三行是问题。 The last row is for the answer.最后一行是答案。

So, in tableView:cellForRowAtIndexPath , you're going to have to examine indexPath.row , and determine whether it is a question row or the answer row.因此,在tableView:cellForRowAtIndexPath ,您将不得不检查indexPath.row ,并确定它是问题行还是答案行。 It should be fairly easy to do, and I'll leave that as an exercise.这应该很容易做到,我将把它留作练习。

There is a problem with your approach if the number of rows in your table exceeds the number that can fit on screen.如果表中的行数超过屏幕上可以容纳的行数,则您的方法存在问题。 In that case, the cells that scroll off-screen will be re-used, and the contents of the button will be lost.在这种情况下,滚动到屏幕外的单元格将被重新使用,并且按钮的内容将丢失。 If you can be sure that this will never happen, use the separate datasource array for button title.如果您可以确定这永远不会发生,请为按钮标题使用单独的datasource数组。

You have to create a separate array with titles of buttons and you can use them against indexPath.row .您必须创建一个带有按钮标题的单独数组,并且可以将它们用于indexPath.row Hope this helps希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM