【发布时间】:2015-11-25 16:49:08
【问题描述】:
目前我有一个带有自定义单元格的uitableview。
它包含一个uilabel 和uibutton - 从两个单独的数组中获取的数据源。
uibutton 函数是在对应的数组中追加uilabel 的行,并将它们插入到uitableview 中,并在下面添加一个带有另一个uibutton 的新单元格。
有一个条件 - 一旦问题数组值为 nil,就会显示答案 uibutton。
问题是 - 因为 uibutton 名称是作为数组中的最后一个值提取的,一旦我滚动所有按钮标题都将被重写以匹配最新的。
这是我的代码
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 值或防止单元格被完全重用。
关于我的问题\答案模型的更多数据 我有一个带有嵌套数组的字典,称为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)。
启动时会加载“第一个”问题和答案,然后根据新附加单元格中的uibutton currentTitle。
谢谢。
【问题讨论】:
-
请出示您的
tableView:numberOfSections和tableView:numberOfRowsForSection:代码,以便我们查看您的问题和答案如何映射到(部分和)行。 -
@PetahChristian 这样做。以及在我的模型上添加信息。
-
您需要显示
someTagsArray,以便我们可以看到问题和答案是如何添加到该数组中的。因为您需要做的是从someTagsArray[indexPath.row]中提取答案文本并将其分配给该行的答案按钮。 -
@PetahChristian 它们看起来像这样
someTagsArray = [question 1, question 2, question 3]; answersdict = [answer1]附加数组后它们看起来像这样someTagsArray = [question1, question 2, question 3, question 1, question 2, question 3]; answersdict = [answer1, answer 2]等
标签: ios swift uitableview reusability