TableView 选定的单元格 - 当滚动到视线之外并选中新单元格时,复选标记消失
TableView selected cells - checkmarks disappear when scrolled out of sight and new cell checked
我有一个 tableViewCell,它在单元格的 accessoryType
中使用了复选标记。我有一个函数,可以将单元格的内容放入 textField 中,并在未选中时类似地从文本字段中删除文本。
它似乎工作正常但如果我检查一个单元格并想检查一个不可见的单元格(IOW)我需要滚动 tableView,被检查的单元格(现在不可见)似乎取消选中自己(仅当我检查一个新的可见单元格时)。
multi select 仅适用于可见单元格。
这是我的代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath)
let row = indexPath.row
cell.textLabel?.text = painArea[row]
cell.accessoryType = .None
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//selectedRow = indexPath
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let row = indexPath.row
let cell = tableView.cellForRowAtIndexPath(indexPath)
if cell!.accessoryType == .Checkmark {
cell!.accessoryType = .None
} else {
cell!.accessoryType = .Checkmark
}
populateDescription()
print(painArea[row])
}
var painDescription = ["very sore"]
func populateDescription() {
painDescription.removeAll()
severityText.text = ""
for cell in tableView.visibleCells {
if cell.accessoryType == .Checkmark {
painDescription.append((cell.textLabel?.text)! + " ")
}
var painArea = ""
var i = 1
while i <= painDescription.count {
painArea += painDescription[i-1] + "~"
i = i + 1
}
severityText.text = painArea
}
我希望我能充分解释自己。我不希望取消选中不可见的单元格,从而将其从我的文本字段中删除,除非我取消选中它。
如有任何想法,我们将不胜感激。
亲切的问候
韦恩
这是因为每当您将单元格滚动出视图并向后滚动时,它会再次调用 cellForRow
并将所有内容设置回默认值,您需要做的是创建一个正确的数据源,每当一个单元格得到检查,你用 Bool 更新数据源表明它已被检查,然后将它设置回 cellForRow
或 cellWillDisplay
很高兴,因为Cell的可重用性。不要在 didSelect
中设置 Checkmark
,而是尝试在 cellForRowAtIndexPath
中设置。您还需要像这样创建 model
class 来解决您的问题
class ModelClass: NSObject {
var isSelected: Bool = false
//Declare other property that you are using cellForRowAtIndexPath
}
现在在 cellForRowAtIndexPath
中检查这个 isSelected
,如下所示。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath)
let row = indexPath.row
let modelClass = painArea[row]
cell.textLabel?.text = modelClass.name
if modelClass.isSelected {
cell.accessoryType = .Checkmark
}
else {
cell.accessoryType = .None
}
return cell
}
现在像这样改变你的 didSelect
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var modelClass = painArea[indexPath.row]
modelClass.isSelected = !modelClass.isSelected
self.tableView.reloadData()
populateDescription()
print(painArea[row])
}
希望对您有所帮助。
我有一个 tableViewCell,它在单元格的 accessoryType
中使用了复选标记。我有一个函数,可以将单元格的内容放入 textField 中,并在未选中时类似地从文本字段中删除文本。
它似乎工作正常但如果我检查一个单元格并想检查一个不可见的单元格(IOW)我需要滚动 tableView,被检查的单元格(现在不可见)似乎取消选中自己(仅当我检查一个新的可见单元格时)。
multi select 仅适用于可见单元格。
这是我的代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath)
let row = indexPath.row
cell.textLabel?.text = painArea[row]
cell.accessoryType = .None
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//selectedRow = indexPath
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let row = indexPath.row
let cell = tableView.cellForRowAtIndexPath(indexPath)
if cell!.accessoryType == .Checkmark {
cell!.accessoryType = .None
} else {
cell!.accessoryType = .Checkmark
}
populateDescription()
print(painArea[row])
}
var painDescription = ["very sore"]
func populateDescription() {
painDescription.removeAll()
severityText.text = ""
for cell in tableView.visibleCells {
if cell.accessoryType == .Checkmark {
painDescription.append((cell.textLabel?.text)! + " ")
}
var painArea = ""
var i = 1
while i <= painDescription.count {
painArea += painDescription[i-1] + "~"
i = i + 1
}
severityText.text = painArea
}
我希望我能充分解释自己。我不希望取消选中不可见的单元格,从而将其从我的文本字段中删除,除非我取消选中它。
如有任何想法,我们将不胜感激。
亲切的问候
韦恩
这是因为每当您将单元格滚动出视图并向后滚动时,它会再次调用 cellForRow
并将所有内容设置回默认值,您需要做的是创建一个正确的数据源,每当一个单元格得到检查,你用 Bool 更新数据源表明它已被检查,然后将它设置回 cellForRow
或 cellWillDisplay
很高兴,因为Cell的可重用性。不要在 didSelect
中设置 Checkmark
,而是尝试在 cellForRowAtIndexPath
中设置。您还需要像这样创建 model
class 来解决您的问题
class ModelClass: NSObject {
var isSelected: Bool = false
//Declare other property that you are using cellForRowAtIndexPath
}
现在在 cellForRowAtIndexPath
中检查这个 isSelected
,如下所示。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath)
let row = indexPath.row
let modelClass = painArea[row]
cell.textLabel?.text = modelClass.name
if modelClass.isSelected {
cell.accessoryType = .Checkmark
}
else {
cell.accessoryType = .None
}
return cell
}
现在像这样改变你的 didSelect
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var modelClass = painArea[indexPath.row]
modelClass.isSelected = !modelClass.isSelected
self.tableView.reloadData()
populateDescription()
print(painArea[row])
}
希望对您有所帮助。