我有一个包含 3 个部分(服务、可用性和天数)的表格视图,每个部分都有多行。我实现了在选择任何行时查看复选标记。现在,我想在第一次关闭表格后再次显示表格时再次查看选定的行。就像显示所选项目的历史一样。
这是显示复选标记的代码:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let indPath = indexPath
let section = indPath.section
switch section {
case 0:
updateCell(indPath, select: true)
case 1:
updateCell(indPath, select: true)
case 2:
updateCell(indPath, select: true)
default:
Void()
}
}
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
let indPath = indexPath
let section = indPath.section
switch section {
case 0:
updateCell(indPath, select: false)
case 1:
updateCell(indPath, select: false)
case 2:
updateCell(indPath, select: false)
default:
Void()
}
}
func updateCell(indexpath: NSIndexPath, select: Bool) {
if let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: indexpath.row, inSection: indexpath.section)) {
if select{
cell.accessoryType = .Checkmark
}
else {
cell.accessoryType = .None
}
}//end if cell
现在在这里我从选定的行中获取数据的方法:
func save(){
if let indexPaths = self.tableView.indexPathsForSelectedRows{
for indexPath in indexPaths{
switch indexPath.section{
case SectionIndex.Services.rawValue:
switch indexPath.row{
case ServicesIndex.Recieve.rawValue:
selectedServices.insert("RECEIVE")
case ServicesIndex.Send.rawValue:
selectedServices.insert("SEND")
default:
Void()
}//end switch
case SectionIndex.Availability.rawValue:
switch indexPath.row{
case AvailabiltiyIndex.Morning.rawValue:
selectedAvailabilities.append(6.0...9.0)
case AvailabiltiyIndex.Regular.rawValue :
selectedAvailabilities.append(9.0...18.0)
case AvailabiltiyIndex.Evening.rawValue:
selectedAvailabilities.append(18.0...21.0)
default:
Void()
}//end switch
case SectionIndex.Days.rawValue:
switch indexPath.row{
case DaysIndex.Sunday.rawValue:
selectedDays.insert("Sunday")
case DaysIndex.Monday.rawValue:
selectedDays.insert("Monday")
case DaysIndex.Tuesday.rawValue:
selectedDays.insert("Tuesday")
case DaysIndex.Wednesday.rawValue:
selectedDays.insert("Wednesday")
case DaysIndex.Thursday.rawValue:
selectedDays.insert("Thursday")
case DaysIndex.Friday.rawValue:
selectedDays.insert("Friday")
case DaysIndex.Saturday.rawValue:
selectedDays.insert("Saturday")
default:
Void()
}//end switch
default:
Void()
}//end switch
}//end for
}//end If
}//end save